Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Reg expression for SSN and Tax ID

Avatar

Level 6

Hi,

I'm looking for a way to validate that a user has entered a value matching either XXX-XX-XXXX or XX-XXXXXXX format. Ideally, the field will only allow numeric and "-" characters to be entered, but if that's too difficult...I could live with it simply ensuring that either format was entered (perhaps on the Exit event).

The script below seems to work HOWEVER it allows the user to enter the Tax ID with 8 digits following the "-" instead of a max of 7 digits. So basically, it accepts XX-XXXXXXX (9 X's) AND XX-XXXXXXXX (10 X's) and it shouldn't allow the latter.

var r = new RegExp();

r.compile("[0-9]{3}\-[0-9]{2}\-[0-9]{4}");

 

var s = new RegExp();

  s.compile("[0-9]{2}\-[0-9]{7}");

var result = r.test(this.rawValue);

var result2 = s.test(this.rawValue);

if (result !== true & result2 !== true  )

{

xfa.host.messageBox("Please enter your SS # in XXX-XX-XXXX format, or a Tax ID in XX-XXXXXXX format");

}

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi there,

don't forget to mention to your regular expression that you want your text to end exactly with these characters...

right now, your regular expression validate if it is true that the value entered contains exactly either (XXX-XX-XXXX) OR (XX-XXXXXXX)

After this being validated it doesn't verify whether if the text ends there or not, as long as this is what it contains it's good with it...

If you want to make sure that you have a maximum length in your string you want it to be entered, don't forget the use of $

Hope this help!

View solution in original post

2 Replies

Avatar

Correct answer by
Level 10

Hi there,

don't forget to mention to your regular expression that you want your text to end exactly with these characters...

right now, your regular expression validate if it is true that the value entered contains exactly either (XXX-XX-XXXX) OR (XX-XXXXXXX)

After this being validated it doesn't verify whether if the text ends there or not, as long as this is what it contains it's good with it...

If you want to make sure that you have a maximum length in your string you want it to be entered, don't forget the use of $

Hope this help!