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”]

[/code]

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="..  >

  • http://www.jak.cx/ Jak

    Sam:
    You should insert this after line 10.

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

  • http://www.jak.cx/ Jak

    A “concise” version.. http://paste.jak.cx/2

    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=="");
    }