SSN regex validation is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2023 02:36 AM - edited 05-09-2023 02:37 AM
hi @Ankur Bawiskar ,
pattern = /^(?!.*(\d{3}[ *]\d{2}[ *]\d{4}|\d{3}[.-]?\d{2}[.-]?\d{4}|\d{3}\.\d{2}\.\d{4}|\d{9}|\d{3}\/\d{2}\/\d{4})).*$/;
we have to abort the action if the pattern is like the below:
999-99-9999
999*99*9999
999 99 9999
999.99.9999
999999999
999/99/9999
this regex is working fine for above formats but it is also aborting the 10 digits number with below formats as well. it should only abort the SSN number format.
0.010638298 and 12345678890
thankyou.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2023 10:37 PM
The regular expression you provided is using a negative lookahead to match patterns that do not match the SSN format. However, it is also excluding other patterns that do not match the SSN format but are valid, such as 10 digit numbers.
To match only the SSN format and exclude other valid patterns, you can use the following regular expression:
ruby
Copy code
^(?!000)(?!666)[0-8]\d{2}(?!00)\d{2}(?!0000)\d{4}$
This regular expression uses negative lookaheads to exclude SSN numbers that start with "000" or "666" and contains all the necessary digits for a valid SSN number. This will allow you to match only the SSN format and exclude other valid patterns.
Alternatively, you could modify your existing regular expression to include additional checks to exclude other valid patterns while still excluding SSN numbers. For example, you could modify your regular expression to only match patterns that have spaces, dashes, or slashes in the specified positions:
css
Copy code
^(?!.*(\d{3}[.-]?\d{2}[.-]?\d{4}|\d{3}[ *]\d{2}[ *]\d{4}|\d{3}\/\d{2}\/\d{4})).*$
This regular expression will match patterns that do not contain the SSN format and have spaces, dashes, or slashes in the specified positions. This will exclude other valid patterns while still excluding SSN numbers.
Regards,
Rachel Gomez