duan1396
duan1396
2018-02-03 15:15
浏览 125
已采纳

无法调用javascript函数

I cant seem call the function formValidation from my form. I tried everything but Im pretty sure I'm over-looking something minor

I have to add more text to post this question and hopefully this much is enough

<!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条回答 默认 最新

  • dtnd30892
    dtnd30892 2018-02-03 15:44
    已采纳

    You have to match the string to the regex, not the regex to the string. e.g. str.match(regex); instead of 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>
    
    点赞 评论
  • douxiong4250
    douxiong4250 2018-02-03 15:44

    there is no function like echo in javascript so remove echo function and it will work

    it is a php ffunction to print

    error one:

    <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>

    working one

    <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>

    while using unknown function or syn tax error javascript will not be called

    点赞 评论

相关推荐