JavaScript Form Validation
JavaScript Form Validation
It is important to validate the form submitted by the user because it can have inappropriate values.
So, validation is must to authenticate user.
It is important to validate the form submitted by the user because it can have inappropriate values.
So, validation is must to authenticate user.
It is important to validate the form submitted by the user because it can have inappropriate values.
So, validation is must to authenticate user.
It is important to validate the form submitted by the user because it can have inappropriate values.
So, validation is must to authenticate user.
Using client side JavaScript is an efficient way to validate the user input in web applications. When there are
many fields in the form, the JavaScript validation becomes too complex.
The JavaScript class presented here makes the form validations many times easier.
The idea is to create a set of "validation descriptors" associated with each element in a form. The "validation
descriptor" is nothing but a string specifying the type of validation to be performed.
Each field in the form can have 0, 1, or more validations. For example, the input should not be empty, should be
less than 25 chars, should be alpha-numeric, etc
A particular aspect of validation in Javascript will be discussed in this section, and that is forms.
An example from real life will help us to understand the situation. As you might have filled out a lot of forms
on the web applications, let's say you are trying to login into the application and you entered the incorrect email
address or you did not provide a password, then you will be given an error message that the email is invalid or
the password field is empty. All of these kinds of validations are performed by Javascript.
We can create an HTML form by using the HTML tags related to forms, like, form, input, etc.
<form>
</form>
Scripts are small pieces of code written in Javascript either in a script tag or in a separate file for some short
automated process. With the Javascript form validation script, we can validate several input fields to ensure that
user data is authentic. The script specifies when a request is to be sent to the server, obviously after the
validations of user data.
In this example, we will check for several form input fields like,
Name should neither be null or blank nor consists of less than 3 characters.
Password should neither be null or blank nor consists of less than 6 characters. Also, both passwords
must match.
Image must be of gif, png, or jpeg extension.
Phone Number must contain all numeric values.
Email must be correct(Further explanation of provided regex is given below).
This javascript function will accept a name variable and will check whether it is null or blank, subsequently, it
will check for the length of the name, which should not be less than 3.
function validateName(name) {
return false;
if (name.length < 3) {
return false;
}
return true;
return false;
return true;
This function will check whether the given input is a number or not. In javascript, we have an inbuilt
function isNaN() which returns true if the provided argument is a number.
function validateNumber(number) {
if (isNaN(number)) {
return false;
return true;
This function will accept an email string as a parameter and test it against a regex pattern to ensure that provided
string is a valid email. Below is the description of RegExp Pattern,
function validateEmail(email)
if (!reg.test(email)) {
return false;
return true;
NOTE:
Client-side validations are performed in order to protect our server from invalid data.
A server cannot respond correctly if the incoming data is not well-formatted; it is, therefore,
necessary to validate the data.
In Javascript, we can validate a form by writing a script and then attaching the script to the
submit button's on-click event.
If the script can determine the validity of the data, then only the server requests will be executed.
EXAMPLE:
<!DOCTYPE html>
<html>
function validateName(name) {
return false;
if (name.length < 3) {
return false;
return true;
return false;
if (password.length < 6) {
return false;
console.log(password, confirm_password)
return true;
}
else {
return false;
function validateImage(file) {
if (!allowedExtensions.exec(fileName)) {
return false;
return true;
function validateEmail(email) {
if (!reg.test(email)) {
return false;
return true;
function displayFileName(file) {
document.getElementById("fileDescription").innerHTML = fileName;
document.getElementById("fileDescription").style = "color:blue";
}
function validateNumber(number) {
if (isNaN(number)) {
return false;
return true;
function validateImage(filePath) {
if (!allowedExtensions.exec(filePath)) {
return false;
return true;
function validateForm() {
if (!validateName(document.signup.fname.value)) {
isValid = false;
if (!validateName(document.signup.lname.value)) {
isValid = false;
if (!validatePassword(document.signup.password.value, document.signup.confirm_password.value))
{
isValid = false;
if (!validateEmail(document.signup.email.value)) {
isValid = false;
if (!validateNumber(document.signup.phone.value)) {
isValid = false;
if (!validateImage(document.signup.file.value)) {
isValid = false;
</script>
<body>
<div>
<hr>
<div class="flex">
<div class="flexItem">
</div>
<div class="flexItem">
</div>
</div>
<div class="flex">
<div class="flexItem">
</div>
</div>
<div class="flex">
<div class="flexItem">
</div>
</div>
<div class="flex">
<div class="flexItem">
</div>
<div class="flexItem">
</div>
</div>
<div class="flex">
<div class="flexItem">
<button class='upload'
onclick="event.preventDefault(); document.getElementById('file').click()">Profile
Picture</button>
</div>
</div>
</div>
</div>
</div>
</form>
</body>
</html>
<html>
<script language="javascript">
function f1(w)
var exp=/[\w]{6,9}[@][\w]{4,5}[.][a-z]{3,4}$/;
var res=exp.test(w);
if(!res)
{
return false;
else
return true;
function f2(x)
var exp=/^[\d]{6,8}$/;
var res=exp.test(x);
if(!res)
return false;
else
return true;
function f3(y)
var exp=/^$/;
var res=exp.test(y);
if(!res)
return false;
else
return true;
function f4(z)
var exp=/(?=^.{9,}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s)[0-9a-zA-Z]*$/;
var res=exp.test(z);
if(!res)
alert(" username must contain atlest one digit ,one small ,one capital letter and must contain more than eight
characters");
return false;
else
return true;
function fun(e,p,um,u)
var em=e.value;
var pwd=p.value;
var empt=um.value;
var usr=u.value;
if((em=="")&&(pwd=="")&&(empt=="")&&(usr==""))
return false;
if(em=="")
return false;
if(pwd=="")
return false;
/*if(empt!="")
}*/
if(usr=="")
return false;
a=f1(em);
b=f2(pwd);
c=f3(empt);
d=f4(usr);
if((b==true)&&(d==true)&&(c==true)&&(a==true))
alert("ok");
return true;
else
alert("not ok");
return false;
</script>
</head>
<body >
Email:<br>
Password:<br>
Empty:<br>
Username:<br>
<input type="text" name="user"><br>
</form>
</body>
</html>
Or
(?=.*\d) asserts that the password must contain at least one digit.
(?=.*[a-z]) asserts that the password must contain at least one lowercase English character.
(?=.*[A-Z]) asserts that the password must contain at least one uppercase English character.
(?=.*[!@#$%^&*()\-+.]) asserts that the password must contain at least one special character from the
specified set.
.{6,20} specifies that the password must be between 6 and 20 characters in length.
function validateEmail(email)
return false;
return true;
/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/