Solved

to allow only characters in my textfield

Posted on 2014-03-27
11
256 Views
Last Modified: 2014-03-28
Hi,

I have this text field that it should only take characters.

 <INPUT style="WIDTH: 100px" id=TicketNumberTxt class=DisableTicketNumberTxt value="" name=TicketNumberTxt>


<input type="submit" name="SubLogEntry" id="SubLogEntry" value="Submit Log" onclick="return Validation();">


function Validation()
{
  var chars = /[,\/\w]/i; // all valid characters
  var value = $("#TicketNumberTxt").val();
  var char = value[value.length-1];
  if (!chars.test(char)) {
    $(this).val(value.substring(0, value.length-1));

return false;
  }
}

Open in new window

0
Comment
Question by:lulu50
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39959891
You have jquery in your tags so
http://jsfiddle.net/GaryC123/9MRJG/2/

$(document).ready(function(){
  $('#TicketNumberTxt').bind('keyup blur', function(){
     $(this).val( $(this).val().replace(/[^a-zA-Z]/g,'') );
    });
});

Open in new window


Edit
What do you mean by only characters?
If you only want numbers then
$(this).val( $(this).val().replace(/[^0-9]/g,'') );

If only numbers and letters then
$(this).val( $(this).val().replace(/[^\w]/g,'') );
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39959907
/[,\/\w]/i will search for a single character from:
,
/
a-z
A-Z
0-9
_

so chars.test(char) will return true if any of those characters are found in the char string.

Btw, \w is already case insensitive, so the "i" is not needed.

HTH,
Dan
0
 
LVL 4

Expert Comment

by:ravikantninave
ID: 39959931
function lettersOnly(evt) {
        evt = (evt) ? evt : event;
        var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
          ((evt.which) ? evt.which : 0));
        if (charCode == 32)
            return true;
        if (charCode > 31 && (charCode < 65 || charCode > 90) &&
          (charCode < 97 || charCode > 122)) {
            return false;
        }
        else
            return true;
    }


<INPUT style="WIDTH: 100px" id="TicketNumberTxt" class="DisableTicketNumberTxt" value="" name="TicketNumberTxt" onkeypress="return lettersOnly(event)" >
0
 

Author Comment

by:lulu50
ID: 39961433
ravikantninave,

I like what you have but I don't numbers only not letters.
0
 
LVL 4

Expert Comment

by:ravikantninave
ID: 39961438
What u need only numbers or chars
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:lulu50
ID: 39961444
only numbers
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39961446
If you want only numbers, modify your code like this:
var chars = /^[0-9]+$/;

Open in new window


If you want only letters, use this
var chars = /^[a-zA-Z]+$/;

Open in new window

0
 
LVL 4

Accepted Solution

by:
ravikantninave earned 500 total points
ID: 39961473
function isNumber(evt) {
        evt = (evt) ? evt : window.event;
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57)) {
            return false;
        }
        return true;
    }

<asp:TextBox ID="txtAmount" runat="server" onkeypress="return isNumber(event)"></asp:TextBox>
0
 

Author Comment

by:lulu50
ID: 39961556
ravikantninave,

It works!!!!!

Thank you for your help!!!!!
0
 

Author Closing Comment

by:lulu50
ID: 39961559
Excellent ++++
0
 
LVL 4

Expert Comment

by:ravikantninave
ID: 39961565
Welcome
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now