Possible Duplicate:
How can i use preg_match in jQuery?
What is the jquery equivalent of the PHP preg_match feature? In PHP it would be :
preg_match('/[^a-zA-Z0-9]/', $str);
Which checks if the string has anything other than letters and numbers. I'd like to add some client sided validation to my site, but I've looked and looked and can't quite find the jQuery equivalent of this. Thanks.
The preg_match() function returns whether a match was found in a string.
Return Values ¶ preg_match() returns 1 if the pattern matches given subject , 0 if it does not, or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false .
In plain JavaScript (no jQuery needed for this), you would just use the .match()
method of the string
object which will return null
if no matches and an array if there are matches:
var str = "myteststring";
if (str.match(/[^a-zA-Z0-9]/)) {
// contains illegal characters
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With