I want to validate text-boxes in JQuery bit I want to use regex for this. As i am new to JQuery, can someone suggest me how regex can be used in JQuery to validate text string (where for e.g. regex is something like ^(?:[\\w\\s-])++$)?
3 Answers
Javascript regex functionality does not require JQuery, it can be used whether you have JQuery loaded or not.
Here are two great resources for javascript regex:
http://www.regular-expressions.info/javascript.html
http://www.w3schools.com/jsref/jsref_obj_regexp.asp
To fit this into JQuery:
if (/your regex/.test($("#textbox").val()))
{
// Valid
}
else
{
// Invalid
}
Comments
1 Comment
Regular expressions aren't part of the jQuery framework, they're just part of javascript. To make them, either call the RegExp constructor, and pass your pattern as a string, or use the shorthand notation:
var newExp = new RegExp('^[0-9]{3,5}$');
//is the same as
var newExp = /^[0-9]{3,5}$/;
There is no need to escape backslashes twice in your example if you meant \w or \s. You might think about reading some tuts on regex, though as your pattern is, erm, flawed...
.val()with Javascript RegExp functions. A real good online tester with cheatsheet is regex.larsolavtorvik.com