在提交表单上调用javascript函数

前端之家收集整理的这篇文章主要介绍了在提交表单上调用javascript函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在提交表单时调用 JavaScript函数.

这是代码但是在提交未调用函数时,请提出建议并且我想使用javascript方法显示错误消息,如何在使用JavaScript的验证中显示错误消息.

<form id="register" name="register"  onsubmit="validateForm()">
<label for="Username"> Username </label><br>
<input type="text" class="register-control" id="Username" name="Username" placeholder="Enter Username"> <br><br>
<label for="Password"> Password </label><br>
<input type="password" class="register-control" id="Password" name="Password" placeholder="Enter Password"><br><br>
<label for="Confirm-Password"> Confirm Password </label><br>
<input type="password" class="register-control" id="Confirm-Password" name="Confirm-Password" placeholder="Confirm Password" ><br><br>

<label for="email"> Email </label><br>
<input type="email" class="register-control" id="email" name="email" placeholder="Enter Valid Email"><br><br>
<button type="submit">Submit</button>

</form>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
 <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
    $("#register").validate({
        rules: {
            "Username": {
                required: true,},"Password": {
                required: true,minlength: 5
            },"Confirm-Password": {
                required: true,"email": {
                required: true,}
        }
    });
});
</script>

这是JavaScript代码

function validateForm()
{
    var password = document.forms["register"]["Password"].value;
    var con-password = document.forms["register"]["Confirm-Password"].value;
    if(password != con-password)
    {
        document.getElementById('password-error').style.visibility='visible';
        alert("not matched");
    }
    alert("matched");
}

解决方法

这可能是由于脚本中存在语法错误.当您看到类似的错误时,请查看浏览器的JavaScript控制台.

在这种情况下,con-password不是有效的变量名. JavaScript看到的是:

var con - password ...

代码说“减去con的密码”.请尝试使用下划线:

var con_password ...

猜你在找的HTML相关文章