What is Form Validation?
Form validation is the process of checking that a form has been filled in correctly before it is processed, this is called form validation.
What You Need to Know before From Validation?
There are main two methods of validating form:
- Server side method
- Client side method
- Basic Validation – First of all, the form must be checked to make sure data was entered into each form field that required it. This would need just loop through each field in the form and check for data.
- Data Format Validation – Secondly, the data that is entered must be checked for correct form and value. This would need to put more logic to test correctness of data.
Let’s build a simple form with a validation script. The form will include more than one input box, select-box, checkbox, radio-button, drop-down-box, message-box, reset-button and a submit button. Our validation script will ensure that the users enter their proper information before the form is sent to the server.
Validating an HTML form using OOJS is out of the scope of this article. We will surely write on that soon.
First we need to create a form using HTML and CSS (only for presentation purposes). If you are not good in HTML, I would recommend you our Advanced HTML5 and CSS3 Course.
Below I used few attributes in first line inside <form> tag e.g. method, action, onsubmit=”return validation(this) in HTML first line which is necessary for Form:
First telling you what is a method?
Method is the action that can be performed on objects through which a browser accomplishes some useful tasks such as validation response, security, and sends form information to the server.
There are two types of methods: –
Post method: Post method is used for sending information to server. All programmers use mainly post method because it is secure and can transfer unlimited information.
Get method: Get method is used for sending information to server too. Get method is less-secure than Post method.
Action: Action is an form attribute where we assign a PHP file generally and this file capture the data submitted from this form and then validate it for the further uses.
<form method="post" action="https://www.admecindia.co.in" onsubmit="return validation(this)"> Name:<input type="text" name="name" /> Phone:<input type="text" name="phone" /> Email:<input type="text" name="email" /> Gender: <input type="radio" value="male" name="gender" >Male <input type="radio" value="female" name="gender" >Female Reference: <input type="checkbox" value="tv" name="tv" >TV <input type="checkbox" value="radio" name="radio" >Radio Course: <select name="course"> <option value="">Select Please</option> <option value="Graphic">Graphic</option> <option value="Web">Web</option> <option value="Animation">Animation</option> </select> Comments: <textarea rows="5" cols="10" name="comm"></textarea> <input type="submit" value="Submit"> <input type="reset" value="Reset"> </form>