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*$)/; **
7
  • 1
    How about this.. /^([a-zA-Z]{2}\d{6}[a-zA-Z])$/ ? Commented Feb 3, 2015 at 6:25
  • 1
    yep, whqat's wrong with /^([a-zA-Z]{2}\d{6}[a-zA-Z])$/ ? Why you added a space \d{6} ? Commented Feb 3, 2015 at 6:27
  • 2 letters, followed by 6 numbers, and Last 1 is letter again. I guess we are not looking at proper length Commented Feb 3, 2015 at 6:27
  • i think the above regex would satisfy your condition. Commented Feb 3, 2015 at 6:28
  • @NileshMendhe What do you mean by not looking at proper length? As asked 9 characters with 2 letters, followed by 6 numbers, followed by one letter Commented Feb 3, 2015 at 6:31

3 Answers 3

1
/\b[A-z]{2}[0-9]{6}[A-z]{1}$/.test('AB332211C')
Sign up to request clarification or add additional context in comments.

Comments

1

This will help..

/^[ ]*([a-zA-Z]{2}\d{6}[a-zA-Z])[ ]*$/

[a-zA-Z]{2} matches two alphabetic character

\d{6} matches subsequent 6 digits

[a-zA-Z] matches one alphabetic character

3 Comments

should also handle blank space before and after
@NileshMendhe actual space added at start and end, but this will make compulsory to have spaces at both ends...Does that comply with requirement?
I mean, it should also match if I put any blank spaces before or after ---- Like we handle using .TRIM() usually. Thnx.
0

/^([a-zA-Z]{2}\d{6}[a-zA-Z])$/

1st Capturing group ([a-zA-Z]{2}\d{6}[a-zA-Z])
[a-zA-Z]{2} match a single character present in the list below

Quantifier: {2} Exactly 2 times
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)

\d{6} match a digit [0-9]
Quantifier: {6} Exactly 6 times

[a-zA-Z] match a single character present in the list below
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
$ assert position at end of the string

Check this, show with all explanation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.