AKor AKor - 25 days ago 7
jQuery Question

How to check for alphanumeric characters

I'm writing a custom method for a jQuery plugin:

jQuery.validator.addMethod("alphanumeric", function(value, element) {
return this.optional(element) || (/*contains "^[a-zA-Z0-9]*$"*/);
});


I know the regexp for what I want, but I'm not sure how to write something in JS that will evaluate to True if it contains the alphanumeric characters. Any help?

Answer

See test RegExp method.

jQuery.validator.addMethod("alphanumeric", function(value, element) {
        return this.optional(element) || /^[a-zA-Z0-9]+$/.test(value);
});