9
$("#user").keyup(function(e){ 
    var regx = /^[A-Za-z0-9]+$/;
    if (!regx.test('#user')) 
    {$("#infoUser").html("Alphanumeric only allowed !");}
);}

#user is a text input, and I want to diplay a warning if user enters anything except letters and numbers.
In the above case, the warning is present whatever is typed.

4
  • with e.g. texotela.co.uk/code/jquery/numeric Commented Jan 15, 2013 at 10:56
  • 2
    You're testing your regex on the string #user itself, not of the content/value of the element that have the id user. !regx.test($(e).val()) should suit your needs. Commented Jan 15, 2013 at 10:57
  • Not an answer, but just a reminder: Don't depend on this if you're submitting values into a database. Javascript can easily be disabled. Commented Jan 15, 2013 at 11:01
  • @andy, thanks. I have similar check on php side. I just want to diplay this warning before submitting. Commented Jan 15, 2013 at 11:05

5 Answers 5

14

change:

if (!regx.test('#user')) 

to

if (!regx.test( $(this).val() ) ) 

Do:

$("#user").keyup(function(e){     
    var str = $.trim( $(this).val() );
    if( str != "" ) {
      var regx = /^[A-Za-z0-9]+$/;
      if (!regx.test(str)) {
        $("#infoUser").html("Alphanumeric only allowed !");
      }
    }
    else {
       //empty value -- do something here
    }
});

JS Fiddle example

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks to everyOne. It works. But, if the input is empty - warning is also displayed !
4

You must test with #user element value not '#user' string

$("#user").keyup(function(e){ 
    var regx = /^[A-Za-z0-9]+$/;
    if (!regx.test($('#user').val()))  // .
    {$("#infoUser").html("Alphanumeric only allowed !");}
);}

Comments

2

THis line

regx.test('#user')

has you testing the string #user, and that is a string that has a bad character (the #). So it will always say not allowed.

Use the actual value of your $("#user") there by using $(this).val()

Comments

1
$('#alpha').bind('keypress', function (event) {
var regex = new RegExp("^[a-zA-Z\b]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
   event.preventDefault();
   return false;
}
});

$('#numeric').bind('keypress', function (event) {
var regex = new RegExp("^[0-9\b]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
   event.preventDefault();
   return false;
}
});

$('#alphanumeric').bind('keypress', function (event) {
var regex = new RegExp("^[a-zA-Z0-9\b]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
   event.preventDefault();
   return false;
}
});


$('#alphanumericspecial').bind('keypress', function (event) {
var regex = new RegExp("^[a-zA-Z0-9 .]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
   event.preventDefault();
   return false;
}
});

1 Comment

A short summary would help reviewers (like me) to judge the validity of your answer; interested users wouldn't have to dive into the code to understand your approach. Just a sentence or two would be fine and not too much work.
0
   function isNotAlphanumeric(){
        return (! val.match(/^[a-zA-Z]+$/))
        //return val.match(/^[a-zA-Z0-9]+$/) ? false : true;
   } 

so if val is alpha numeric it return false. So based on returned value take appropriate action. You can call this method on key up event.

1 Comment

return (! val.match(/^[a-zA-Z]+$/)) would be a bit cleaner

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.