
A while ago I saw somebody had a formvalidator in combination with classes/dom scripting. I was impressed by this, so I thought "hmm maybe I can create something like that also".
So I made a javascript class to handle the form validation.
<form id="frmContact" action="">
</form>
<form id="frmContact" action="">
<input type="text" name="text_name" id="text_name" value="">
</form>
Notice that I've named the inputfield "text_name" and the id also. The "text_" prefix is needed.
<form id="frmContact" action="">
<label id="label_name" for="text_name">Name</label>
<input type="text" name="text_name" id="text_name" value="">
</form>
<form id="frmContact" action="">
<fieldset id="fieldset_name">
<label id="label_name" for="text_name">Name</label>
<input type="text" name="text_name" id="text_name" value="">
</fieldset>
</form>
<form id="frmContact" action="">
<input type="hidden" name="cssformvalidator_required_textfields" id="cssformvalidator_required_textfields" value="name">
<fieldset id="fieldset_name">
<label id="label_name" for="text_name">Name</label>
<input type="text" name="text_name" id="text_name" value="">
</fieldset>
</form>
Notice that I've named the inputfield "cssformvalidator_required_textfields"!!!
<form id="frmContact" action="">
<input type="hidden" name="cssformvalidator_required_textfields" id="cssformvalidator_required_textfields" value="name">
<input type='hidden' name="cssformvalidator_required_email" id="cssformvalidator_required_email" value="email">
<fieldset id="fieldset_name">
<label id="label_name" for="text_name">Name</label>
<input type="text" name="text_name" id="text_name" value="">
</fieldset>
</form>
Notice that I've named the inputfield "cssformvalidator_required_email"!!!
<input type='hidden' name="cssformvalidator_show_alert" id="cssformvalidator_show_alert" value="true"/>
Name is required!
But wat if you want to alter that? Just insert the folowing hidden-field: <input type='hidden' name="cssformvalidator_alert_text" id="cssformvalidator_alert_text" value="_fieldname_ is required you stupid!!">
Also notice the _fieldname_ string. The script text will get the value within the label tag en _fieldname_ will be replaced with that value.
window.onload=function()
{
var frm=document.getElementById("frmContact");
frm.onsubmit=function(){
var myValidator=new CssFormValidator(this);
return myValidator.validateForm();
}
}
So the javascript class need the form ID-name. So everytime you submit the form, the onsubmit function will be fired.
And to download the script: cssformvalidator.js