GabAntonelli
GabAntonelli

Reputation: 767

how to exclude special characters from regex?

I'm a very rookie with regexs, but this is very simple and i can't understand why it allows special characters like quotation marks or dots before the string when i use them in jquery validation plugin for validating a form. I need the input to be 6 characters (3letters then 3 numbers)

here's the code

     $(document).ready(function()
{
$.validator.addMethod(
    "regex",
    function(value, element, regexp) {
        var re = new RegExp(regexp);
        return this.optional(element) || re.test(value);
    },
    "Please check your input."
);
$('#taxistep1').validate({
    rules:{
        'targa':{
            required: true,
            regex: "[A-Za-z]{3}[0-9]{3}"
        }
    },

    messages:{
        'targa':{
            required: "Inserire il numero di targa",
            regex: "Formato targa non valido"
        }
    }
});
});

Upvotes: 2

Views: 1971

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 148990

Try using start (^) and end ($) anchors to match the beginning and end of the input string, respectively:

regex: "^[A-Za-z]{3}[0-9]{3}$"

This will ensure that no extra characters appear before or after the matched portion of the input.

Upvotes: 2

Related Questions