HTML form validation can be done by JavaScript.
Basic Validation
If a form field (fname) is empty, this function alerts a message, and returns false, to prevent the form from being submitted:
Example(1)
I have also create email and password fields
function validate() {
if( document.myForm.Name.value == "" ) {
alert( "Please provide your name!" );
document.myForm.Name.focus() ;
return false;
}
if( document.myForm.EMail.value == "" ) {
alert( "Please provide your Email!" );
document.myForm.EMail.focus() ;
return false;
}
if( document.myForm.Password.value == "" || isNaN( document.myForm.Password.value ) ||
document.myForm.Password.value.length != 8 ) {
alert( "Please provide a 8 characters " );
document.myForm.Password.focus() ;
return false;
}
return( true );
}
<form action = "backup.php" name = "myForm" onsubmit = "return(validate());">
<div class="form-group col-sm-12">
<label for="Name">Name</label>
<input id="Name" type="text" class="form-control ">
</div>
<div class="form-group col-sm-12">
<label for="EMail">EMail</label>
<input id="EMail" type="text" class="form-control ">
</div>
<div class="form-group col-sm-12">
<label>Password </label>
<input class="form-control " type="Password" name="Password" >
</div>
<div class="form-group text-center">
<input class="btn btn-primary" type="submit" >
</div>
</form>
In above example also validating numeric input.
Automatic HTML Form Validation
HTML form validation can be performed automatically by the browser:
If a form field (fname) is empty, the required
attribute prevents this form from being submitted:
<form action="/backup.php" method="post">
<input type="text" name="fname" required>
<input type="submit" value="Submit">
</form>
Data Validation
data that is entered must be checked for correct form and value. Your code must include appropriate logic to test correctness of data.
Typical validation tasks are:
has the user filled in all required fields?
has the user entered a valid date?
has the user entered text in a numeric field?
Most often, the purpose of data validation is to ensure correct user input.
Validation can be defined by many different methods, and deployed in many different ways.
Server side validation is performed by a web server, after input has been sent to the server.
Client side validation is performed by a web browser, before input is sent to a web server.
Example(2)
<script type = "text/javascript">
// Form validation code will come here.
function validate() {
if( document.myForm.Name.value == "" ) {
alert( "Please provide your name!" );
document.myForm.Name.focus() ;
return false;
}
var emailID = document.myForm.EMail.value;
at_position = emailID.indexOf("@");
dot_position = emailID.lastIndexOf(".");
if (at_position < 1 || ( dot_position - at_position < 2 )) {
alert("Please enter correct email ID")
document.myForm.EMail.focus() ;
return false;
}
if( document.myForm.Password.value == "" || isNaN( document.myForm.Password.value ) ||
document.myForm.Password.value.length != 8 ) {
alert( "Please provide a 8 characters " );
document.myForm.Password.focus() ;
return false;
}
return( true );
}
</script>
HTML Constraint Validation
HTML5 introduced a new HTML validation concept called constraint validation.
HTML constraint validation is based on:
Constraint validation HTML Input Attributes
Constraint validation CSS Pseudo Selectors
Constraint validation DOM Properties and Methods
Constraint Validation HTML Input Attributes
Attribute | Description |
---|---|
disabled | Specifies that the input element should be disabled |
max | Specifies the maximum value of an input element |
min | Specifies the minimum value of an input element |
pattern | Specifies the value pattern of an input element |
required | Specifies that the input field requires an element |
type | Specifies the type of an input element |
Complete Code for Form validation In Java script
<!DOCTYPE html>
<html>
<head>
<title>Form Validation Using HTML And JavaScript</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="text-center">
<h1>Form Validation Using HTML And JavaScript</h1>
</div>
<br>
<div class="well">
<form action = "backup.php" name = "myForm" onsubmit = "return(validate());">
<div class="form-group col-sm-12">
<label for="Name">Name</label>
<input id="Name" type="text" class="form-control ">
</div>
<div class="form-group col-sm-12">
<label for="EMail">EMail</label>
<input id="EMail" type="text" class="form-control ">
</div>
<div class="form-group col-sm-12">
<label>Password </label>
<input class="form-control " type="Password" name="Password" >
</div>
<div class="form-group text-center">
<input class="btn btn-primary" type="submit" >
</div>
</form>
<div class="form-group col-sm-2"></div>
</div>
<br>
</div>
</body>
</html>
<script type = "text/javascript">
// Form validation code will come here.
function validate() {
if( document.myForm.Name.value == "" ) {
alert( "Please provide your name!" );
document.myForm.Name.focus() ;
return false;
}
var emailID = document.myForm.EMail.value;
at_position = emailID.indexOf("@");
dot_position = emailID.lastIndexOf(".");
if (at_position < 1 || ( dot_position - at_position < 2 )) {
alert("Please enter correct email ID")
document.myForm.EMail.focus() ;
return false;
}
if( document.myForm.Password.value == "" || isNaN( document.myForm.Password.value ) ||
document.myForm.Password.value.length != 8 ) {
alert( "Please provide a 8 characters " );
document.myForm.Password.focus() ;
return false;
}
return( true );
}
</script>