admin管理员组

文章数量:1318570

Can any body tell me what is the Regex validation to use my textbox should take only letters and Numbers?

  var BLIDRegExpression = /^[a-zA-Z0-9]*$/;

    if (BLIDRegExpression.test(BLIDIdentier)) {
        alert('The BLID Identifier may only consist of letters or numbers and must be exactly five characters long.');
        return false;
    }

I am using this one but its not working. can anybody tell me.

Thanks

Can any body tell me what is the Regex validation to use my textbox should take only letters and Numbers?

  var BLIDRegExpression = /^[a-zA-Z0-9]*$/;

    if (BLIDRegExpression.test(BLIDIdentier)) {
        alert('The BLID Identifier may only consist of letters or numbers and must be exactly five characters long.');
        return false;
    }

I am using this one but its not working. can anybody tell me.

Thanks

Share Improve this question asked Jan 21, 2012 at 16:32 user957178user957178 6314 gold badges16 silver badges27 bronze badges 2
  • whats the value for BLIDIdentier? – marcio Commented Jan 21, 2012 at 16:37
  • by the way, you can use /^[a-zA-Z0-9]{5}$/ to check that the string is exactly 5 characters long too, if you want (to test the second half of your error message too) – Dexter Commented Jan 21, 2012 at 16:39
Add a ment  | 

2 Answers 2

Reset to default 5

.test returns true if the string matches. I think you want:

var BLIDRegExpression = /^[a-zA-Z0-9]{5}$/; // {5} adds the requirement that the string be 5 chars long

if (!BLIDRegExpression.test(BLIDIdentier)) {
    alert('The BLID Identifier may only consist of letters or numbers and must be exactly five characters long.');
    return false;
}

Reverse your logic. .test() returns true when it matches, false when it does not match. You want to execute your if statement when it does NOT match. If you also want it to be required to be exactly 5 characters long, then you can use {5} instead of * in your regex like this:

var BLIDRegExpression = /^[a-zA-Z0-9]{5}$/;

if (!BLIDRegExpression.test(BLIDIdentier)) {
    alert('The BLID Identifier may only consist of letters or numbers and must be exactly five characters long.');
    return false;
}

本文标签: javascriptjquery REGEX validation for letters and Numbers onlyuStack Overflow