In jQuery, is there a function/plugin which I can use to match a given regular expression in a string?
For example, in an email input box, I get an email address, and want to see if it is in the correct format. What jQuery function should I use to see if my validating regular expression matches the input?
I've googled for a solution, but I haven't been able to find anything.
If you wanted to search some elements based on a regex, you can use the filter
function. For example, say you wanted to make sure that in all the input boxes, the user has only entered numbers, so let's find all the inputs which don't match and highlight them.
$("input:text")
.filter(function() {
return this.value.match(/[^\d]/);
})
.addClass("inputError")
;
Of course if it was just something like this, you could use the form validation plugin, but this method could be applied to any sort of elements you like. Another example to show what I mean: Find all the elements whose id
matches /[a-z]+_\d+/
$("[id]").filter(function() {
return this.id.match(/[a-z]+_\d+/);
});
I'm using jQuery and JavaScript and it works fine for me:
var rege = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if(rege.test($('#uemail').val())){ //do something }
I believe this does it:
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
It's got built-in patterns for stuff like URLs and e-mail addresses, and I think you can have it use your own as well.
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