Last active
August 22, 2019 14:18
-
-
Save eeskildsen/c37b8a5d2f10d2e3a5e624fb6e7da8a2 to your computer and use it in GitHub Desktop.
Parsley U.S. phone number validator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input type="tel" name="phone" id="phone" class="form-control" required="" data-parsley-phone=""> | |
<script> | |
var parsleyPhoneValidatorSettings = { | |
ignoreCharacters: /[()+\- ]/g, | |
usPhoneNumber: /^1?\d{10}$/ | |
}; | |
window.Parsley.addValidator('phone', { | |
validateString: function(value) { | |
return parsleyPhoneValidatorSettings.usPhoneNumber.test(value.replace(parsleyPhoneValidatorSettings.ignoreCharacters, '')); | |
}, | |
messages: { | |
en: 'This value should be a phone number.' | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment