Simple Javascript Form Validator
JavaScript can be used to validate data in HTML forms before sending off the content to a server.
Form data that typically are checked by a JavaScript could be:
- has the user left required fields empty?
- has the user entered a valid e-mail address?
- has the user entered text in a numeric field?
Initial and most simple approach for the javascript form validation is by checking each fields in the form with their required values and generating alert box . To code for the large forms will be tedious and complexity also increases subsequently. So I thought to make a simple and general javascript form validator without the use of any javascript frameworks and images so that it can deployed easily.
How it Works:
If any field is found empty , or if email is not valid etc. then it displays the respective message next to the form elements where the validation is required.
How to Use:
First download the formValidator.js from the link below.
Include the js file before the head tag.
<script src='form_validator_compressed.js'><script>
Just give id of the form as ‘myForm‘
if the form elements value should not be empty then give class as ‘required’ for that particular field.
class = 'required'
if value should be valid email:
class='validate'
if value should be number:
class='number'
if the browsed file should be image:
class='image'
if passwords of two password field need to be same:
class='confirm'
to both of the password fields
Drawbacks:
It doesnot work on the radio button.
if other additional function is required that is related to the that particular form where this validator is used, it can give problem since the code runs on winow.onload event.
If there is any bugs with the validtor or if any additional function is required or any queries is regarding the form validator then please feel free to email me on prabeen.giri@gmail.com
Demo
Download Link:formValidator.js


this validation is really very good and useful. it will be more better if u add 1 more option for telephone validation like : +977 01-234234 should accept space and + symbol
@prasanna thanks for your response. Please provide the specific format of what u want to validate i.e +977 01-9841000000 or +97701-9841000000 or +977019841000000 or all. Please let me know then I can provide new js file with your functionality.
all it should accept all three format ..
@prasanna ok i have made some changes to the js validator file to meet your requirement.
It not just validate the mobile format u mentioned i.e +977 01-9841000000 and +97701-9841000000 and +977019841000000 but also 977019841000000 . This one is bonus for you. Please use class=’nepalimobile’ to validate above numbers.
Note:If you are using this validator then please use class ‘email’ to validate email but not ‘validate’ becuase i have done some modifications and enhancements on the code.
Here is the link for new validator.
thanks bro… can u modify it for land line number also..
cuz peoplemay not alway put their mobile no. while filling the form. any ways u made a gr8 javascript validation.
hey bro this validation doesn’t work for tag…..check it out..
why ?