[ACCEPTED]-How to check for alphanumeric characters-jquery
Accepted answer
See test
RegExp method.
jQuery.validator.addMethod("alphanumeric", function(value, element) {
return this.optional(element) || /^[a-zA-Z0-9]+$/.test(value);
});
0
If you want to use Spanish chars in your 3 alphanumeric validation you can use this:
jQuery.validator.addMethod("alphanumeric", function(value, element) {
return this.optional(element) || /^[a-zA-Z0-9áéíóúÁÉÍÓÚÑñ ]+$/.test(value);
});
I 2 also added a blank space to let users add 1 words
You can use regexes in JavaScript:
if( yourstring.match(/^[a-zA-Z0-9]+/) ) {
return true
}
Note that 2 I used +
instead of *
. With *
it would return 1 true if the string was empty
// use below ... It is better parvez abobjects.com
jQuery.validator.addMethod("postalcode", function(postalcode, element) {
if( this.optional(element) || /^[a-zA-Z\u00C0-\u00ff]+$/.test(postalcode)){
return false;
}else{
return this.optional(element) || /^[a-zA-Z0-9]+/.test(postalcode);
}
}, "<br>Invalid zip code");
rules:{
ccZip:{
postalcode : true
},
phone:{required: true},
This will validate zip code having no letters but alphanumeric
0
$("input:text").filter(function() {
return this.value.match(/^[a-zA-Z0-9]+/);
})
0
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.