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.

Author: Mukesh Jakhar

This is Mukesh Jakhar. I’m a professional web developer with 3+ years experience. I am always ready for freelance work and i am writing blogs in my free time. I love to learn new technologies and share with others.
I founded PHPKIDA in September 2015. The focus of this website to provide web development tutorials of PHP, WordPress, CodeIgniter, Jquery, MySQL, HMTL, CSS etc and sharing solution of problems which i already solved.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

How to get the value of HTML tag using JS

JavaScript timer countdown days hours minutes seconds

JavaScript validation on image field

html5 video tag Complete Guide

Getting The Screen Resolution Using JavaScript

How to create textbox in html dynamically using javascript

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Free online tool to download Instagram videos, photos and save Instagram videos, photos to your device. It's free & easy to use.

Download Now

CURRENCY CONVERTER

Our real time currency converter calculator will convert your money based on current values from around the world. It's free & easy to use.

Convert Now

ROBOST.TXT GEN.

Create robots txt file that help search engines to crawling and indexing your site with help of our free online Robots.txt Generator tool.

Create Now

HTML MINIFIER

Free online tool to minify or compress html code and reduce the size of html code in few clicks. It's free & easy to use.

Minify Now

CSS MINIFIER

Free online tool to minify or compress css code and reduce the size of css code in few clicks. It's free & easy to use.

Minify Now

JAVASCRIPT MINIFIER

Free online tool to minify or compress javascript code and reduce the size of javascript code in few clicks. It's free & easy to use.

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.