我正在尝试在提交表单时调用
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"); }