complete user registration form validation using java script with example

It is important to validate the form submitted by the user because it can have inappropriate values. So validation is must.
The JavaScript provides facility to us validate or apply validation form at the client side so processing will be fast than server-side validation. So, most of the web developers prefer JavaScript form validation.
Through JavaScript, we can validate name, password, email, date, mobile number etc fields.
Let’s start apply java script validation on form step by step.

1. Java Script Name Validation, In this tutorial you will learn how to validate name field, first we are checking name field is empty or not, if field is empty return error and if field not empty then we will again check name field must be accept only alphabets.

Java Script Code Here

 <script>
function Submit(){
    var nameRegex = /^[A-Z a-z]*$/;
    var name = document.Registerform.Name.value;
        
    if( name == "" )
   {
     document.Registerform.Name.focus() ;
     document.getElementById("errorBox").innerHTML = "enter the full name";
     return false;
   } 
   
   else if(!nameRegex.test(name)){
        document.Registerform.Name.focus();
        document.getElementById("errorBox").innerHTML = "Enter only alfabets";
        return false;
     }    
</script>

1. Java Script Email Id Validation, In this tutorial you will learn how to validate Email field, first we are checking email field is empty or not, if field is empty return error and if field not empty then we will again check using the email regular expression, email must be have in correct format.

Java Script Code Here

<script>
function Submit(){
    var emailRegex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    email = document.Registerform.Email.value;
        
   if (email == "" )
    {
        document.Registerform.Email.focus();
        document.getElementById("errorBox").innerHTML = "enter the email";
        return false;
     }else if(!emailRegex.test(email)){
        document.Registerform.Email.focus();
        document.getElementById("errorBox").innerHTML = "enter the valid email";
        return false;
     }
</script>

1. Java Script Phone Number Validation, In this tutorial you will learn how to validate Phone Number field, first we are checking phone number field is empty or not, if field is empty return error and if field not empty then we will again check phone number field must be contain only numbers other values not allowed.

Java Script Code Here

<script>
var mobileRegex = /^[0-9]*$/;
mobileNumber = document.Registerform.mobileNumber.value;
if (mobileNumber == "" )
    {
        document.Registerform.mobileNumber.focus();
        document.getElementById("errorBox").innerHTML = "Enter the mobile number";
        return false;
     }else if(!mobileRegex.test(mobileNumber)){
        document.Registerform.mobileNumber.focus();
        document.getElementById("errorBox").innerHTML = "Accepted only numbers";
        return false;
     }
</script>

1. Java Script Password Validation, In this tutorial you will learn how to validate password field, first we are checking password field is empty or not, if field is empty return error and if field not empty then we will again check password field must be at least 8 char long.

Java Script Code Here

function Submit(){
   password = document.Registerform.Password.value;

if(password == "")
     {
         document.Registerform.Password.focus();
         document.getElementById("errorBox").innerHTML = "enter the password";
         return false;
     }
     else if(password.length<8)
     {
         document.Registerform.Password.focus();
         document.getElementById("errorBox").innerHTML = "Password must be at least 8 char long";
         return false;
     }
</script>

1. Java Script DOB ( Date Of Birth ) Validation, In this tutorial you will learn how to validate select box or DOB field, first we are checking select box field is empty or not, if field is empty return error and if field not empty return true.

Java Script Code Here

 <script>
var month = document.Registerform.birthday_month.value,
    day = document.Registerform.birthday_day.value,
    year = document.Registerform.birthday_year.value;
if (month == "") {
        document.Registerform.birthday_month.focus();
        document.getElementById("errorBox").innerHTML = "select the birthday month";
        return false;
     }
     if (day == "") {
        document.Registerform.birthday_day.focus();
        document.getElementById("errorBox").innerHTML = "select the birthday day";
        return false;
     }
     if (year == "") {
        document.Registerform.birthday_year.focus();
        document.getElementById("errorBox").innerHTML = "select the birthday year";
        return false;
     }
</script>

1. Java Script Radio Button Validation, In this tutorial you will learn how to validate to radio buttons or field, first we are radio button is checked or not if button is not checked return error otherwise return true.

Java Script Code Here

<script>
if(document.Registerform.radiobutton[0].checked == false && document.Registerform.radiobutton[1].checked == false){
                document.getElementById("errorBox").innerHTML = "select your gender";
             return false;
            }
</script>

Complete Form Validation – All In One

JAVA SCRIPT CODE HERE

<script>
function Submit(){
    var emailRegex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var nameRegex = /^[A-Z a-z]*$/;
    var mobileRegex = /^[0-9]*$/;
    var fname = document.Registerform.Name.value,        
        email = document.Registerform.Email.value,
        reemail = document.Registerform.enterEmail.value,
        mobileNumber = document.Registerform.mobileNumber.value,
        password = document.Registerform.Password.value,
        month = document.Registerform.birthday_month.value,
        day = document.Registerform.birthday_day.value,
        year = document.Registerform.birthday_year.value;
        
    if( fname == "" )
   {
     document.Registerform.Name.focus() ;
     document.getElementById("errorBox").innerHTML = "enter the full name";
     return false;
   } 
   
   else if(!nameRegex.test(fname)){
        document.Registerform.Name.focus();
        document.getElementById("errorBox").innerHTML = "Enter only alfabets";
        return false;
     }    
     
   
   if (email == "" )
    {
        document.Registerform.Email.focus();
        document.getElementById("errorBox").innerHTML = "enter the email";
        return false;
     }else if(!emailRegex.test(email)){
        document.Registerform.Email.focus();
        document.getElementById("errorBox").innerHTML = "enter the valid email";
        return false;
     }
     
      if (reemail == "" )
    {
        document.Registerform.enterEmail.focus();
        document.getElementById("errorBox").innerHTML = "Re-enter the email";
        return false;
     }else if(!emailRegex.test(reemail)){
        document.Registerform.enterEmail.focus();
        document.getElementById("errorBox").innerHTML = "Re-enter the valid email";
        return false;
     }
     
     if(reemail !=  email){
         document.Registerform.enterEmail.focus();
         document.getElementById("errorBox").innerHTML = "emails are not matching, re-enter again";
         return false;
         }
     
     
      if (mobileNumber == "" )
    {
        document.Registerform.mobileNumber.focus();
        document.getElementById("errorBox").innerHTML = "Enter the mobile number";
        return false;
     }else if(!mobileRegex.test(mobileNumber)){
        document.Registerform.mobileNumber.focus();
        document.getElementById("errorBox").innerHTML = "Accepted only numbers";
        return false;
     }
     
    if(password == "")
     {
         document.Registerform.Password.focus();
         document.getElementById("errorBox").innerHTML = "enter the password";
         return false;
     }
     else if(password.length<8)
     {
         document.Registerform.Password.focus();
         document.getElementById("errorBox").innerHTML = "Password must be at least 8 char long";
         return false;
     }
     
     if (month == "") {
        document.Registerform.birthday_month.focus();
        document.getElementById("errorBox").innerHTML = "select the birthday month";
        return false;
     }
     if (day == "") {
        document.Registerform.birthday_day.focus();
        document.getElementById("errorBox").innerHTML = "select the birthday day";
        return false;
     }
     if (year == "") {
        document.Registerform.birthday_year.focus();
        document.getElementById("errorBox").innerHTML = "select the birthday year";
        return false;
     }
        if(document.Registerform.radiobutton[0].checked == false && document.Registerform.radiobutton[1].checked == false){
                document.getElementById("errorBox").innerHTML = "select your gender";
             return false;
            }
        if(fname != '' && email != '' && reemail != '' && password != '' && month != '' && day != '' && year != ''){
            document.getElementById("errorBox").innerHTML = "Registerform submitted successfully";
            }
          
}

</script>

HTML CODE HERE

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>complete user registration form validation using javascript with example - phpkida.com</title>
</head>

<body>


<div id="container">
  <div id="container_body">
    <div>
      <h2 class="form_title">User Registration Form</h2>
      <p class="head_para">Complate User Registration Form Validated Using Javascript</p>
    </div>
    
      
       <form name="Registerform" method="post">
       <div id="errorBox"></div>
       <div id="comman">
        <input type="text" name="Name" value="" placeholder="Full Name"  class="input_text">        
      </div>
      
      <div id="comman">
        <input type="text" name="Email" value=""  placeholder="Your Email" class="input_text">
      </div>
      
      <div id="comman">
        <input type="text" name="enterEmail" value=""  placeholder="Re-enter Email" class="input_text">
      </div>
      
      <div id="comman">
        <input type="text" name="mobileNumber" value=""  placeholder="Mobile Number" class="input_text">
      </div>
      
      <div id="comman">
        <input type="password" name="Password" value=""  placeholder="New Password" class="input_text">
      </div>
      
      <!--birthday details start-->
      <div>
        <h4>Select Your DOB</h4>
      </div>
      <div>
      
           <select name="birthday_day" >
          <option value="" selected>Day</option>
          <script>
              var i=1;
              for(i;i<=31;i++)
              {
              document.write("<option value='"+i+"'>"+i+"</option>");
              }
              </script>
             </select>
        </select>
        &nbsp;&nbsp;
        
        <select name="birthday_month" >
          <option value="" selected >Month</option>
          <script>
              var i=1;
              for(i;i<=12;i++)
              {
              document.write("<option value='"+i+"'>"+i+"</option>");
              }
              </script>
        </select>
        &nbsp;&nbsp;
       
        <select name="birthday_year">
          <option value="" selected>Year</option>
          <script>
              var i=2010;
              for(i;i>=1960;i--)
              {
              document.write("<option value='"+i+"'>"+i+"</option>");
              }
              </script>
        </select>
      </div>
      <!--birthday details ends-->
      <div id="radio_button">
      
          <input type="radio" name="radiobutton" value="Male">
        <label >Male</label>
        &nbsp;&nbsp;&nbsp;
        <input type="radio" name="radiobutton" value="Female">
        <label >Female</label>       
        
      </div>
       <div>
        <input type="submit" name="submit" value="Submit" id="sign_user" onClick="return Submit();">
      </div>
     </form>
    </div>
  </div>
</body>
</html>

CSS CODE HERE

<style>

body{
    font-family:Tahoma, Geneva, sans-serif;
    margin:0px;
    padding:0px;
    }
#container{
    width:550px;
    background-color:rgba(255,255,200,.5);
    margin:auto;
    margin-top:10px;
    margin-bottom:10px;
    box-shadow:0 0 3px #999;
    }
#container_body{
    padding:20px;
    }
.form_title{
    font-size:30px;
    color:#141823;
    text-align:center;
    padding:10px;
    font-weight:normal;
    margin:0px;
    }
.head_para{
    font-size:18px;
    color:#99a2a7;
    text-align:center;
    font-weight:normal;
    }

.input_text{
    width:100%;
    padding:5px;
    font-size:18px;
    }
#comman{
    clear:both;
    padding:15px 0 10px 0px;
    }
select{
    padding:5px;
    }
#birthday{
    font-size:12px;
    color:#8b919d;
    padding-top:10px;
    }
#radio_button{
    padding:10px 0 0 0;
    }
#sign_user{
    font-size:16px;
    font-weight:bold;
    color:#FFF;
    text-align:center;
    background-color:#009999;
    padding:10px 40px;
    margin-top:10px;
    cursor: pointer;
    border:1px solid #333;
    }
#errorBox{
    color:#F00;
    }

</style>

Click on Image and download Source Code of This Tutorial

Download source code of complete user registration form validation using java script with example

I hope you like this Article. Share with your friends.

Leave a Reply

Related Posts: You may like

How To Create a Modal Popup With CSS and JavaScript

Get Latitude Longitude from Address

cookie policy popup

How to make a sticky sidebar on scroll

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Download Instagram Videos & Photos.

Download Now

CURRENCY CONVERTER

Real Time Currency Converter

Convert Now

ROBOST.TXT GEN.

Robots.txt File Creater

Create Now

HTML MINIFIER

Compress yout HTML Code

Minify Now

CSS MINIFIER

Compress yout CSS Code

Minify Now

JAVASCRIPT MINIFIER

Compress yout JavaScript Code

Minify Now

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.