Skip to content

Instantly share code, notes, and snippets.

@sayanriju
Created September 27, 2016 08:09
Show Gist options
  • Save sayanriju/9fcc6fd0405de9eb1044a5ff997437c8 to your computer and use it in GitHub Desktop.
Save sayanriju/9fcc6fd0405de9eb1044a5ff997437c8 to your computer and use it in GitHub Desktop.
A collection of simple validators for using with html forms
var validateEmail = function (email) {
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
var validateAlphanumeric = function (txt) {
var re = /^[a-z0-9]*$/i;
return re.test(txt);
}
var validateNumeric = function (txt) {
var re = /^[0-9]*$/i;
return re.test(txt);
}
var validateLength = function (txt, len) {
return (txt.length === len);
}
var validateRequired = function (txt) {
return (txt !== undefined && txt !== null && txt.trim() !== "");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment