Regular expression field validation in jQuery Regular expression field validation in jQuery jquery jquery

Regular expression field validation in jQuery


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 }


Unless you're looking for something specific, you can already do Regular Expression matching using regular Javascript with strings.

For example, you can do matching using a string by something like this...

var phrase = "This is a phrase";phrase = phrase.replace(/is/i, "is not");alert(phrase);

Is there something you're looking for other than just Regular Expression matching in general?