Skip to content

Instantly share code, notes, and snippets.

@eeskildsen
Last active August 22, 2019 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eeskildsen/c37b8a5d2f10d2e3a5e624fb6e7da8a2 to your computer and use it in GitHub Desktop.
Save eeskildsen/c37b8a5d2f10d2e3a5e624fb6e7da8a2 to your computer and use it in GitHub Desktop.
Parsley U.S. phone number validator
<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