sjstroot
sjstroot

Reputation: 11

regex for pattern validation using html in coldfusion

I have a variable "CustCd" that is 7 digits long. The first digit can be either a number or letter but the last 6 digits (positions 2-7) have to be numeric. I am trying to use a regex where pattern = "[0-9 A-Z a-z]{1} [0-9]{2,7}" with class validate required and it is not working.

Here is my code:

<input type="text" name="CustCd" id="CustCd" 
    size="7" maxlength="7" 
    pattern="[0-9 A-Z a-z]{1} [0-9]{2,7}" 
    title="Customer Code" class="validate required" />

This is my first time using regexes so I think I may be overlooking something. Any help greatly appreciated.

Upvotes: 1

Views: 832

Answers (1)

Matt Busche
Matt Busche

Reputation: 14333

Try using ^[a-zA-Z0-9]{1}[0-9]{6}$ as your pattern. Remember this validation is done against the JavaScript regEx engine.

<input type="text" name="CustCd" id="CustCd" size="7" maxlength="7" 
  pattern="^[a-zA-Z0-9]{1}[0-9]{6}$"  title="Customer Code" class="validate required" />

Also make sure you have the correct doctype on the page

<!DOCTYPE html>

Explanation via regex101.com

^ assert position at start of the string
[a-zA-Z0-9]{1} match a single character present in the list below
  Quantifier: {1} Exactly 1 time (meaningless quantifier)
  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)
  0-9 a single character in the range between 0 and 9
[0-9]{6} match a single character present in the list below
  Quantifier: {6} Exactly 6 times
  0-9 a single character in the range between 0 and 9
$ assert position at end of the string

Full sample code to try

<!DOCTYPE html>
<form>
  <input type="text" name="CustCd" id="CustCd" size="7" maxlength="7" pattern="[a-zA-Z0-9]{1}[0-9]{6}" title="Customer Code" class="validate required" />
  <input type="submit">
</form>

Upvotes: 2

Related Questions