Free Form Validation JavaScript What's so special about this free form validation JavaScript? First of all it checks the fields while you are filling out the fields. The script masters three different tasks:
- It checks whether the required textfields are filled out. - It validates the email address. - It checks whether the textfield contains numbers only.
The free form validation script works fine with nternet Explorer 5x/6x/7x/8x/9x, Firefox 2x/3x, Opera 8x/9x/10x/11x, Google Chrome 3x/4x/5x/6x/7x/8x.
Demo Form
Use the demo form below and fill out the fields with incorrect and correct values in order to see how it works.
Step 2: Let's have a look at the sample form below, in order to set the mandatory form-fields.
email address:
<input type="text" name="email" class="isnotok" style="width:300px" onblur="return check_email(this,'email address:\nPlease enter a valid email address')">
Validation: does the field contain a valid email address?
ZIP:
<input type="text" name="ZIP" class="isnotok" style="width:300px" onblur="return check_numbers(this,'ZIP:\nOnly positive numbers and zero are allowed')">
Validation: does the textfield contain numbers only?
city:
<input type="text" name="city" class="isnotok" style="width:300px" onblur="return check_filledout(this,'city:\nPlease fill out the name of the city')">
Validation: is the textfield filled out?
comment:
<textarea name="comment" class="isnotok" rows="6" style="width:300px" onblur="return check_filledout(this,'comment:\nPlease enter your comment')">
</textarea>
Validation: is the textarea-field filled out?
sex:
<input type="radio" name="sex" value="Male" onclick="return check_filledout(this)">Male <input type="radio" name="sex" value="Female" onclick="return check_filledout(this)">Female
Validation: has a radio button been selected?
onsubmit="return check_final(this)"
The red element within <form>-tag calls the final validation check. It is absolutely mandatory! It should not be changed.
name="anyname"
Each field requires a unique name.
class="isnotok"
Each textfield and each textarea requires this class.
Do not change anything.
The class ist not required for type "radio" and "selection"
onblur="return check_email(this,'add your error message')
Use this if you want to apply an email validation check.
Can be used for field-type "text" or field "textarea".
Light green marks the text that appears when the email is not valid.
onblur="return check_filledout(this,'add your error message')"
Use this if you want to check whether the field is filled out.
Can be used for field-type "text" or field "textarea".
Light green marks the text that appears when the field is not filled out correctly.
onclick="return check_filledout(this)"
Use this for field-type "radio" and field-type "selection".
No comment reqeuired.
onblur="return check_numbers(this,'add your error message')"
Use this if you want to check whether the field consists of numbers only.
Can be used for field-type "text" or field "textarea".
Light green marks the text that appears when the field is not filled out correctly.
Step 3: Go between the <style>-tag </style>-tag and configure the style according to the instructions.
Step 4: Go on top of the <script>-code and enter the names of the form-fields (required fields only) according to the instructions.