Regular expression field validation in jQuery
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 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+/);
});