简体   繁体   中英

This is my javascript code for checking regular expression like username validation , password but not getting any output

I have written a code to validate a registration form this is my javascript there is no ouput when I run the whole html form along with the javascript any username, mobile, password gets accepted it does not gives any message although I have given a error message to display if it does not match the regex

 <script type = "text/javascript">
     function validation(){
     var name = document.getElementById('name').value;
     var username = document.getElementById('username').value;
     var email = document.getElementById('email').value;
     var phone = document.getElementById('phone').value;
     var password = document.getElementById('password').value;
     var confirmpassword = document.getElementById('confirmpassword').value;
    
     var namecheck =/^[ a-zA-Z\-\']+$/;
     var usercheck = /^[a-z0-9_\.]+$/;
     var passwordcheck = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,15}$/;
     var emailcheck =/^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+$/;
     var mobilecheck = /^(0|[+91]{3})?[7-9][0-9]{9}$/;
    
     if(namecheck.test(name)){
         document.getElementById('nameerror').innerHTML = " ";
     }
    
     else{
          document.getElementById('nameerror').innerHTML = "** Invalid name";
          return false;
    
     }
    
    
     if(usercheck.test(username)){
         document.getElementById('usererror').innerHTML = " ";
     }
    
     else{
          document.getElementById('usererror').innerHTML = "** Invalid user name";
          return false;
    
     }
    
    
     if(passwordcheck.test(password)){
         document.getElementById('passworderror').innerHTML = " ";
     }
    
     else{
          document.getElementById('passworderror').innerHTML = "** password err";
          return false;
    
     }
     }
    </script> 

See the pattern whether it is correct or not but I have checked in regex101 it works it matches the characters but not working when I am executing with html

You might want to check how the validate() function is called - does it actually execute and show the messages, but the form gets submitted anyway?

Given the code you've shared, I take it you're a beginner in Javascript; one simple way to tell if what you're doing is working is to show an alert() statement at the beginning of the validate() function to make sure it's being called. Use alert() calls across the board in your code to see if what you expect to be happening actually is (ie show alerts inside if statements).

Next, learn how to use console.log() to debug inside your browser. Next, learn to use the debugger inside the browser (Chrome has a great debugger).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM