javascript not working properly on 2 forms in same html page

Viewed 38

even when the password has all the correct values !((password.match(passw)) is executing and if i remove the formvalidationlogin.js then also the formvalidationregistartion.js is throwing error for f1 from. when the login and registartion were in seperate html pages these javascript functions worked perfectly.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>signup-login</title>
    <link rel="stylesheet" type="text/css" href="css/index.css">
<link href="https://fonts.googleapis.com/css2?family=Jost:wght@500&display=swap" rel="stylesheet">
</head>
<body>

<div class="main">      
<input type="checkbox" id="chk" aria-hidden="true">
<div class="signup">
<form name="regform" action="registrationsession.php" onsubmit="return Validate()" method="post">
<label for="chk" aria-hidden="true">Sign up</label>
<input type="text" id="username" name="username" placeholder="User name" required="">
<input type="email" id="email" name="email" placeholder="Email" required="">
<input type="password" id="password" name="password" placeholder="Password" required="">
<script type="text/javascript" src="javascript/formvalidationregistration.js"></script>
<input type="submit" name="submit" value="Sign Up" class="login-button">
</form>
</div>

<div class="login">
<form name="f1" action="loginsession.php" onsubmit="return Validate()" method="post">
<label for="chk" aria-hidden="true">Login</label>
<input type="email" name="email" id="email" placeholder="Email" required="">
<input type="password" name="pass" id="pass" placeholder="Password" required="">
<script type="text/javascript" src="javascript/formvalidationlogin.js"></script>
<input type="submit" name="submit" value="Login" class="login-button">  
</form>
</div>
</div>
</body>
</html>
//formvalidationregistration.js

function Validate() {

var password = regform.password.value;

    var name = regform.username.value;

    var passw = "^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$";

    if (!(password.match(passw))){
        alert("Password Should Contain At Least One Uppercase Letter, One Lowercase letter, One Number And One Special Character");
        return false;
    }

    else if(password.length < 8) {
        alert("Password Must Be At least 8 Characters long");
        return false;
    }

    else if(name.length < 6) {
        alert("Username Must Be At least 6 Characters long");
        return false;
    }

    else if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(regform.email.value))) {
        alert("You have entered an invalid email address!");
        return false;
    }
}
//formvalidationlogin.js

function Validate() {

    var password = f1.pass.value;
    
    var passw = "^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$";

    if (!(password.match(passw))) {
        alert("Password Should Contain At Least One Uppercase Letter, One Lowercase letter, One Number And One Special Character");
        return false;
    }

    else if (password.length < 8) {
        alert("Password Must Be At least 8 Characters long");
        return false;
    }
}
0 Answers
Related