There are two methods for validating a form
- Using a keyword “validate” inside the input tag
This Will present output as:-
Complete code for the above form is shared below :-
Presented output for the above operation :-
2. Using function call we need to define a function that will validate the field data and return corresponding errors.
We need to write the html code for input field in the form
<form><label><b> User-Id</b> </label> <input type=”text” name=”userid” placeholder= “User-Id” size=”20″ id=”userId” /> <p id=”error1″></p></form>
First we need to call the function on submit of form as (<form name=”myForm” onsubmit=”return validateForm()” methord=”post”>) Then we need to define the function
This would produce output as
Complete code for the above function is shared below :-
Conclusion:- Validating a form in javascript is easy and is done with the function call and displays an error with respect to the type of input in the field.
[…] Let’s see an example for User form validation in JavaScript. click here. […]