Advertisement
Guest User

Edited Validation

a guest
Jul 19th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 3.93 KB | None | 0 0
  1. <!DOCTYPE html>
  2.  
  3. <html>
  4. <head>
  5.     <title>Form validation using Regex</title>
  6.  
  7.      <!-- Latest compiled and minified CSS -->
  8. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
  9.  
  10. <!-- jQuery library -->
  11. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  12.  
  13. <!-- Popper JS -->
  14. <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
  15.  
  16. <!-- Latest compiled JavaScript -->
  17. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
  18.  
  19. </head>
  20. <body>
  21.  
  22.     <h1 class="text-center bg-dark text-white">Form validation using REGEX in javascript</h1>
  23.  
  24.    <div class="container">
  25.     <br>
  26.     <form>
  27.  
  28.         <div class="form-group">
  29.             <label>Username:</label>
  30.             <input type="text" name="" id="username" class="form-control" >
  31.             <span id="usererror" class="text-danger font-weight-bold"></span>
  32.         </div>
  33.  
  34.         <div class="form-group">
  35.             <label>Password:</label>
  36.             <input type="text" name="" id="password" class="form-control">
  37.             <span id="passerror" class="text-danger font-wright-bold"></span>
  38.         </div>
  39.  
  40.         <div class="form-group">
  41.             <label>Confirm password:</label>
  42.             <input type="text" name="" id="cpassword" class="form-control">
  43.             <span id="cpasserror" class="text-danger font-weight-bold"></span>
  44.         </div>
  45.  
  46.         <div class="form-group">
  47.             <label>Email :</label>
  48.             <input type="text" name="" id="email" class="form-control">
  49.             <span id="emailerror" class="text-danger font-weight-bold"></span>
  50.         </div>
  51.  
  52.         <div class="form-group">
  53.             <label>Mobile no:</label>
  54.             <input type="text" name="" id="mobile" class="form-control">
  55.             <span id="mobileerror" class="text-danger font-weight-bold"></span>
  56.         </div>
  57.  
  58.         <input onclick="validation()" type="button" name="button" value="submit" class="btn btn-danger">
  59.  
  60.    
  61.     </form>
  62. </div>
  63.  
  64. <script type="text/javascript">
  65.  
  66.   function validation(){
  67.    
  68.     var username = document.getElementById('username').value;
  69.     var password = document.getElementById('password').value;
  70.     var cpassword = document.getElementById('cpassword').value;
  71.     var email = document.getElementById('email').value;
  72.     var mobile = document.getElementById('mobile').value;
  73.  
  74.     console.log(username);
  75.  
  76.     var usercheck = /^[A-Za-z. 1-99]{3,85}$/ ;
  77.     var passwordcheck = /^(?=.*[0-9])(?=.*[!@#$%^&*])[A-Za-z0-9!@#$%^&*]{8,18}/;
  78.     var emailcheck = /^[A-za-z0-9_]{3,}@[A-za-z]{3,}[.][A-Za-z.]{2,}$/;
  79.     var mobilecheck = /^[9][0-9]{9}$/;
  80.  
  81.  
  82.    if(usercheck.test(username)){
  83.  
  84.       document.getElementById('usererror').innerHTML =" ";
  85.  
  86.    }
  87.  
  88.     else{
  89.         document.getElementById('usererror').innerHTML ="Username is invalid";
  90.         return false;
  91.     }
  92.  
  93.     if(passwordcheck.test(password)){
  94.        document.getElementById('passerror').innerHTML =" ";
  95.  
  96.     }
  97.  
  98.     else{
  99.         document.getElementById('passerror').innerHTML ="Passowrd is invalid";
  100.         return false;
  101.     }
  102.  
  103.  
  104.    if(password.match(cpassword)){
  105.       document.getElementById('cpasserror').innerHTML =" ";
  106.  
  107.    }
  108.  
  109.    else{
  110.     document.getElementById('cpasserror').innerHTML ="Password donot match";
  111.     return false;
  112.    }
  113.  
  114.     if(emailcheck.test(email)){
  115.        document.getElementById('emailerror').innerHTML =" ";
  116.  
  117.     }
  118.  
  119.     else{
  120.         document.getElementById('emailerror').innerHTML ="Email is invalid";
  121.         return false;
  122.     }
  123.  
  124.      if(mobilecheck.test(mobile)){
  125.        document.getElementById('mobileerror').innerHTML =" ";
  126.  
  127.     }
  128.  
  129.     else{
  130.         document.getElementById('mobileerror').innerHTML ="Passowrd is invalid";
  131.         return false;
  132.     }
  133.  
  134.   }
  135.  
  136.  
  137.  
  138.  
  139. </script>
  140.  
  141. </body>
  142. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement