Part 2 of this tutorial gives code to validate the form designed in part one, stopping spam and fake users submitting data.
Here is the code (it uses javascript);
<script> function validate(form){ var name = form.name.value; var company = form.company.value; var description = form.description.value; var telephone = form.telephone.value; var contact = form.contact.value; var AtPos = contact.indexOf(“@”); var StopPos = contact.lastIndexOf(“.”); if (description.length < 1) { alert(“You must enter a description.”); return false; } if (company.length < 1) { alert(“You must enter a company.”); return false; } if (name.length < 1) { alert(“You must enter a name.”); return false; } if (telephone.length < 11 || telephone.length > 20) { alert(“You must enter a telephone number between 11 and 20 characters.”); return false; } if (contact.length < 1) { alert(“You must enter an email address.”); return false; } if (AtPos == -1 || StopPos == -1) { alert (“Invalid Email Address.”) return false; } if (StopPos < AtPos) { alert (“Invalid Email Address.”) return false; } if (StopPos – AtPos == 1) { alert (“Invalid Email Address.”) return false; } return true; } </script>The first thing that happens is that the function is named and given a single variable, which is called ‘form’. In the form design we used an onsubmit to use ‘this’ (i.e. the form itself) as the variable content.
Function specific variables are defined, calling in the input values of the form.
Now it’s relatively simple; a bunch of IF statements to return false unless the criteria (for example more than one character) is met. If none of the IF’s are called then the function returns true and consequently the form can send! It’s litterally that simple! Have a play with the code, and you will get the hang of it soon enough.
I hope this tutorial helped at least one person, as I looked for ages online to find an all-in-one solution for designing and validating a form with little success!
