0

Following is the requirement of field value should be - else it should generate an error

Format to be only 9 characters with 2 letters, followed by 6 numbers, followed by one letter

e.g. 'AB332211C'

Any else value should generate an error message using JavaScript. Can anyone help me in creating regex expression for this.

EDITS : Till yet I am done with this : Help to improve the same

--------------------------------------------------------------------------
    var myAssumption = /^\d{2}[a-zA-z] \d{6}[0-9]\d{1}[a-zA-z]$/;
--------------------------------------------------------------------------

Following links might help in answering : http://www.java2s.com/Code/JavaScript/Form-Control/Mustbeatleast3charactersandnotmorethan8.htm

Examples :

 // Common regexs
  var regexEmail = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;
  var regexUrl = /^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([\w]+)(.[\w]+){1,2}$/;
  var regexDate = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
  var regexTime = /^([1-9]|1[0-2]):[0-5]\d$/;
  var regexIP = /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
  var regexInteger = /(^-?\d\d*$)/; **

strong text**

1

1 Answer 1

1

Following Regular Expression worked for me -

/\b[A-z]{2}[0-9]{6}[A-z]{1}$/;

Start - /

Handle spaces(TRIM) - \b

First 2 letters - [A-z]{2}

Followed by 6 Numbers - [0-9]{6}

End with 1 letter - [A-z]{1}$/

Hope this explanation will someone in future. Refere site : Learn Regular Expressions

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .