Regular expression to validate the pattern

user3756059

The pattern I need is below:

  • MA should be the first two characters of a string
  • The third character should be hyphen (-)
  • Characters 4 to 10 can be any numeric numbers (0-9)
  • The eleventh character should be hyphen (-)
  • Characters 12 to 15 can be any numeric numbers (0-9)

Example:

MA-1234567-1234

I have tried this:

/^(MA*)[0-9]{7}([0-9]{4})$/
Rory McCrossan

The Regex you were using is missing the dashes between character sets, try this:

/^MA-\d{7}-\d{4}$/

Note that to incorporate this with an input box you would need to test against this Regex on keyup, something like this:

var re = /^MA-\d{7}-\d{4}$/;
$('#code').keyup(function() {
    if (re.test(this.value)) {
        console.log('The code is valid...');
    }
});

Example fiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related