0

I don't know why the regex patterns was not working ! I couldn't find any error ! But when i run this with wrong input it just skips the pattern test line and moves to the next page !

<!DOCTYPE html>
<html>
<head>
    <title>Validation</title>
    <script>
        function validate()
        {
            var email=document.myform.email.value;
            var pass=document.myform.pass.value;
            var phno=document.myform.phno.value;
            if(email!="" && pass!="" && phno!="")
            {
                var emailcheck=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
                var passcheck=/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}/;  
                var phonecheck=/^[0-9]{10}+$/;
                var result1=emailcheck.test(email);
                var result2=passcheck.test(pass);
                var result3=phonecheck.test(phno);
            }
            else
            {
                alert("Validation failed");
                return false;
            }
            if(result1 && result2 && result3)
            {
                alert("Validation Successful");
                //return true;
            }
            else
            {
                    alert("Validation Failed");
                    document.write("email should be as (xxx@gmail.com)"+"<br/>"+"password should be as (pass123)"+"<br/>"+"phone should be as (98659-57575)"+"<br/>");
                    return false;
            }
        }
    </script>
</head>

<body>
    <form name="myform" action="number.php" onsubmit="return validate()">
    Email:<input type="text" name="email"/><br/>
    Password:<input type="text" name="pass"/> <br/>
    Phone:<input type="text" name="phno"/> <br/>
    <input type="submit" value="submit"/>
    </form>
</body>
</html>

I expect the alert validation failed, if I give any input that mismatch the pattern.

  • 1
    1) When you say "it just skips the pattern test line", what do you mean? Why do you think that? 2) What are some example input values that did not work? – Vlad274 Jul 01 '19 at 18:54
  • 1
    If you check browser console when your page loads it returns: 'SyntaxError: nothing to repeat' as https://stackoverflow.com/questions/6288181/javascript-regex-nothing-to-repeat-error, 'You need to double the backslashes used to escape the regular expression special characters'. as I see in your code you have multiple of this mistake. – Pouria Moosavi Jul 01 '19 at 19:01

1 Answers1

1

Your code has regex errors which leads to script fail. as validate script fails the onsubmit would not find validate function and so everything will ruined.
It is a good behavior to check your regex with online regex test sites like this one
I think if you change your regex part of code to this every thing will work correctly:

var emailcheck=/^([A-Za-z0-9_\\-\\.])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,4})$/;
var passcheck=/(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}/;  
var phonecheck=/^[0-9]{10}$/;

As you will notice I've changed every / with //, and also removed + in the last regex, as it was not valid.
I hope it helps.

Pouria Moosavi
  • 662
  • 7
  • 22