Really basic form validation

As I know this will help someone, here’s a simple way to use form validation.

One important bit: The function to go in the <head>.

[code lang=”js”]


And make sure you text boxes have an "ALT" attribute, like this for example:
<input type="text" name="fname" id="fname" alt="First Name" />
Important! Only put an ALT attribute for the required inputs!

Finally, tuck this little bit into your <form> tag you want to use:
<form name="contactform" onsubmit="return(checkForm(this));" method="..  >

  • Jak

    You should insert this after line 10.

    if (document.getElementById('password').value != document.getElementById('pwconfirm').value) {
    message = message + "Error: Passwords don't match!\n";

  • Jak

    A “concise” version..

    function checkForm(f) {
    	m=""; for(i=0;i<f.length;i++) { e=f.elements[i]; 
    	m+=(e.type=="text"&&e.value==""&&e.alt!="")?"Please make sure "+e.alt+" is not empty.\n":""; }
    	if(m!="") alert(m); return (m=="");