无法调用javascript函数

时间:2018-02-03 15:15:32

标签: php html

我似乎无法从我的表单中调用函数formValidation。我尝试了一切,但我很确定我看起来很轻微

我必须添加更多文字来发布这个问题,希望这足够了

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <script>
        function formValidation(){
            var user = document.getElementById("username").value;
            var password = document.getElementById("password").value;
            var userValid = /^[a-zA-Z0-9]{5,10}$/;
                // username must be between 5 to 10 characters and shouldn't contain special characters
            var passwordValid = /^{8,18}$/;
                //password must be atleast 8 characters

            if(!userValid.match(user)){
                alert('Invalid username');
                return false;
            }
            echo('test');
            return true;
        }
    </script>

<h3>Register New User</h3> 




    <form name = "form1" onsubmit="return formValidation() " action="process.php" method="POST" >
<!--      Order matters, first JS script run then the next php page visited -->
        Username:<input type="text" id="username" placeholder="Enter" value="" name="username"> <br>    
        Email ID:<input type="text" name= "email"><br>
        Password: <input type="password" id="password" ><br>
        Confirm Password: <input type="password" id="password" ><br>
                 <input type="submit" name="button" value="Click here">

    </form>




</body>
</html>

2 个答案:

答案 0 :(得分:0)

您必须将字符串与正则表达式匹配,而不是将正则表达式与字符串匹配。例如str.match(regex);代替regex.match(str);

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <script>
        function formValidation(){
            var user = document.getElementById("username").value;
            var password = document.getElementById("password").value;
            var userValid = /^[a-zA-Z0-9]{5,10}$/;
                // username must be between 5 to 10 characters and shouldn't contain special characters
            var passwordValid = /^[a-zA-Z0-9]{8,18}$/;
                //password must be atleast 8 characters

            if(!user.match(userValid)){
                alert('Invalid username');
                return false;
            }
        }
    </script>

<h3>Register New User</h3> 




    <form name = "form1" onsubmit="return formValidation() " action="process.php" method="POST" >
<!--      Order matters, first JS script run then the next php page visited -->
        Username:<input type="text" id="username" placeholder="Enter" value="" name="username"> <br>    
        Email ID:<input type="text" name= "email"><br>
        Password: <input type="password" id="password" ><br>
        Confirm Password: <input type="password" id="password" ><br>
                 <input type="submit" name="button" value="Click here">

    </form>




</body>
</html>

答案 1 :(得分:0)

在javascript中没有像echo这样的函数,所以删除echo函数它会起作用

这是一个打印的PHP函数

错误一:

&#13;
&#13;
<script>
function validateForm() {
    var x = document.forms["myForm"]["fname"].value;
    echo('asd');
    if (x == "") {
        alert("Name must be filled out");
        return false;
    }
    
}
</script>

<body>

<form name="myForm" 
onsubmit="return validateForm()" method="post">
Name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>

</body>
&#13;
&#13;
&#13;

工作一次

&#13;
&#13;
<script>
function validateForm() {
    var x = document.forms["myForm"]["fname"].value;
   
    if (x == "") {
        alert("Name must be filled out");
        return false;
    }
    
}
</script>
<body>

<form name="myForm" 
onsubmit="return validateForm()" method="post">
Name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>

</body>
&#13;
&#13;
&#13; 使用未知函数或syn税错误时,javascript将不会被称为