to allow only characters in my textfield

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

lulu50Asked:
Who is Participating?
 
ravikantninaveCommented:
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
 
GaryCommented:
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
 
Dan CraciunIT ConsultantCommented:
/[,\/\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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
ravikantninaveCommented:
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
 
lulu50Author Commented:
ravikantninave,

I like what you have but I don't numbers only not letters.
0
 
ravikantninaveCommented:
What u need only numbers or chars
0
 
lulu50Author Commented:
only numbers
0
 
Dan CraciunIT ConsultantCommented:
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
 
lulu50Author Commented:
ravikantninave,

It works!!!!!

Thank you for your help!!!!!
0
 
lulu50Author Commented:
Excellent ++++
0
 
ravikantninaveCommented:
Welcome
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.