tags:

views:

56145

answers:

5

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.

+8  A: 

I believe this does it:

http://docs.jquery.com/Plugins/Validation/validate

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.

Daniel Schaffer
But what about general regex matching? Maybe something like searching for a regex in some text etc.
trex279
Well I mean you can always just use javascript's regex straight, if that's all you need to do...http://www.regular-expressions.info/javascript.html
Daniel Schaffer
+11  A: 

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?

Hugoware
Your example will return "This not is a phrase"! :-)
Ian Roke
Funny catch - I never actually ran the code but you're right - It matches on the first 'is' which is actually at the end of the word 'This' - Since it ignores the rest of the string it doesn't do anything to the second 'is'. Very funny :)
Hugoware
+24  A: 

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+/);
});
nickf
+2  A: 

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 }
Klatys
A: 

My code :

$("input.numeric").keypress(function(e) { /* pour les champs qui ne prennent que du numeric en entrée */          
            var key = e.charCode || e.keyCode || 0;                     
            var keychar = String.fromCharCode(key);
            /*alert("keychar:"+keychar + " \n charCode:" + e.charCode + " \n key:" +key);*/
            if (  ((key == 8 || key == 9 || key == 46 || key == 35 || key == 36 || (key >= 37 && key <= 40)) && e.charCode==0) /* backspace, end, begin, top, bottom, right, left, del, tab */
                    || (key >= 48 && key <= 57) ) { /* 0-9 */
                return;
            } else {
                e.preventDefault();
            }
        });
bilelz