Salesforce Steps
Salesforce Steps

Reputation: 183

Validation on Field : JavaScript regex required for Field

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*$)/; **

Upvotes: 0

Views: 563

Answers (3)

Rakesh_Kumar
Rakesh_Kumar

Reputation: 1442

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

Upvotes: 1

H. Mahida
H. Mahida

Reputation: 2366

/^([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.

Upvotes: 0

user2106480
user2106480

Reputation: 181

/\b[A-z]{2}[0-9]{6}[A-z]{1}$/.test('AB332211C')

Upvotes: 1

Related Questions