Skip to content

Instantly share code, notes, and snippets.

@tborychowski
Last active October 11, 2015 14:28
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 tborychowski/3872620 to your computer and use it in GitHub Desktop.
Save tborychowski/3872620 to your computer and use it in GitHub Desktop.
JS :: Simple form validation
function validate(f,v,a,x){
for(a=0;x=f[a++];)
if((v=window[x.getAttribute('valid')])&&!v(x.value)){
alert(x.getAttribute('alert'));
return !x.focus();
}
}
function notempty(x){return x>''}
function ismail(e){return /^[\w\.-]{2,}@[\w\.-]+\.[a-z]{2,5}$/i.test(e)}
// <form onsubmit="return validate()">
// <input valid="notempty" alert="enter name!" type="text" name="name">
// <input valid="isemail" alert="enter email!" type="text" name="email">
// </form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment