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

无法调用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 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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler