开发者

Regex with jquery

I am trying to perform a few check but REGEX I just don't get the hang of. I am trying to check firstly for 3 characters and secondly want to perform a check that there are no numeric characters. The code is below 开发者_开发问答and finally could some one answer is the regex syntax same in every language?

$(document).ready(function() {
    var name=$('#firstname');
    function validateName() {
        //function nameCheck() {
        if (name.val().length <=2) {
            name.addClass('error'); 
            $(".hidden_f").html("First Name must be 3 letters!"); 
        } else if ($("#firstname.").val().match('[a-z]')) {
            $(".hidden_f").html("No numbers!");     
        }
    }
    name.keyup(validateName);
});

And also if possible could some one give me a few examples so I can try and learn regex cheers!


There are a few slightly different flavors of RegExp, but for the most part, RegExp is the same in every language with minor differences.

I think this is the best resource for RegExp : http://www.regular-expressions.info/

it's packed with articles and examples, truly an amazing website.

To match a phrase with 3 or more characters and no numeric characters you could use

var re = /[^0-9]{3,}/i

That should work.


You're passing a string, not a regexp, as the argument of match, so it's trying to Mach the sequence in the string literally as it is. Regexps are delimited by / / or created with new Regexp(escaped string).

As an aside, if you don't need the matches, you should use regexp.test(input) instead.


Firstly you seem to have an error in the selector $("#firstname.") should read $("#firstname")

As for regex - for 3 letters (no numbers) you can use match(/[a-zA-Z]{3}/)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜