compuhelpindia.com

JavaScript Form Validation

Compuhelpindia.com

« Previous                                                                                                                                  Next Chapter »

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.
Eaxmple:
<!DOCTYPE html>
<html>
<body>
<script>
function show()
{
var name,agr;
name=document.getElementById("t1").value;
age=document.getElementById("t2").value;
if(name=="")
{
alert("Enter Your Name");
}
else if(age=="")
{
alert("Enter Your Age");
}
}
</script>
<form>
Name<input type="text" id="t1"><br/>
Age<input type="text" id="t2"><br/>
<button onclick="show()">Click Here</button>
</form>
</body>
</html>  
 
Name
Age
E-mail Validation
The function below checks if the content has the general syntax of an email.
Example:
<!DOCTYPE html>
<html>
<body>
<form>
Email:<input type="email"><br>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>  
 
Fill The Text:
Email: