2

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"
        }
    }
});
});
Sparky
  • 98,165
  • 25
  • 199
  • 285
GabAntonelli
  • 757
  • 2
  • 9
  • 26

1 Answers1

2

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.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331