Reputation: 8970
I am trying to create a quick validation of a text area notes field to see if it potentially contains a SSN number which I will then throw an alert for.
I have tried a few different RegEx patters I have found online and none of them seem to be working. I am wondering if its my javascript that isn't correct?
I believe if it were to find a match, it would be true thus throw the SSN Found
alert.
Anyone point out my mistake?
$(document).ready(function() {
$('[name="submit"]').click(function() {
validate();
})
})
// Validate our note field
function validate() {
var isValid = true,
notes = $('[name=notes]').val(),
ssn = new RegExp('^(?!219-09-9999|078-05-1120)(?!666|000|9\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$');
console.log(notes)
if (ssn.test(notes)) {
alert('SSN Found');
}else{
alert('No SSN Found');
}
}
JS Fiddle: https://jsfiddle.net/sgrw4rqf/2/
Upvotes: 0
Views: 224
Reputation: 3370
You need to make your regex global rather than matching the SSN only.
Use something like this:
if ($('[name=notes]').val().match(/\b(?!219-09-9999|078-05-1120)(?!666|000|9\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4}\b/g)) {
and it will match as expected.
Upvotes: 1