javascript 在 HTML5 中想要验证密码和确认密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21520157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In HTML5 want to validate password and confirm password
提问by Jazz
I want to validate password and confirm password in HTML5. By using function required I have validated for empty value. But not able to validate password and confirm password
我想在 HTML5 中验证密码并确认密码。通过使用所需的函数,我已经验证了空值。但无法验证密码和确认密码
HTML:
HTML:
<form id="registration" name="registration" action="registrationaction.php" method="POST">
<input type="password" class="agent-input registerinput" name="pswd" id="pswd">
<input type="password" class="agent-input registerinput " name="confirmpassword" id="confirmpassword">
</form>
JavaScript:
JavaScript:
<script>
$().ready(function()
{
$("#registration").validate({
rules: pswd: {
required: true
},
confirmpassword: {
required: true
},
},
messages
pswd: "Please enter password",
confirmpassword: "Please enter valid confirm password",
},
submitHandler: function() { $('#registration').submit(); }
});
});
回答by Daniel Rippstein
There are several methods to confirm the two password match. However, assuming you want to utilize the jQuery Validation Plugin (which your question seems to use), changing your Javascript code to this will do the job:
有多种方法可以确认两个密码匹配。但是,假设您想使用 jQuery 验证插件(您的问题似乎使用了该插件),将您的 Javascript 代码更改为此将完成这项工作:
$().ready(function(){
$("#registration").validate({
rules: {
pswd: {
required: true
},
confirmpassword: {
required: true,
equalTo: "#pswd"
}
},
messages: {
pswd: "Please enter password",
confirmpassword: "Please enter valid confirm password"
},
submitHandler: function() { $('#registration').submit(); }
});
});
I should point out that your Javascript example had several issues (unclosed brackets, etc). They've been corrected in the example above.
我应该指出您的 Javascript 示例有几个问题(未封闭的括号等)。它们已在上面的示例中得到纠正。
And for more amazingness you can create with jQuery Validation Plugin, definitely remember to see their documentation webpagefor an easy-to-read list of available options.
如果您想使用 jQuery Validation Plugin 创建更多精彩,请务必记住查看他们的文档网页以获取易于阅读的可用选项列表。
P.S. To everyone insisting that $().ready() must be changed, I'd like to point out that it is perfectly valid syntax according to the jQuery documentation(although not recommended, it is valid).
PS 对于坚持必须更改 $().ready() 的每个人,我想指出根据jQuery 文档,它是完全有效的语法(虽然不推荐,但它是有效的)。
回答by Talha Masood
You should write your code within proper ready() function. I think the code you have provided must be throwing errors. Anyhow two correct ways to do this would be:
您应该在适当的 ready() 函数中编写代码。我认为您提供的代码一定是在抛出错误。无论如何,两种正确的方法是:
$( document ).ready(function() {
//your code comes here
});
$(function() {
//your code comes here
});
Inside these you can write the form validation code. Please be clear on the point that you can only do this with JavaScript.
在这些中,您可以编写表单验证代码。请明确一点,您只能使用 JavaScript 执行此操作。
回答by user2727841
$(document).ready(function() {
$("#registration").validate({
rules: pswd: {
required: true
},
confirmpassword: {
required: true
},
},
messages
pswd: "Please enter password",
confirmpassword: "Please enter valid confirm password",
},
submitHandler: function() { $('#registration').submit(); }
});
});
OR
或者
$(function() {
$("#registration").validate({
rules: pswd: {
required: true
},
confirmpassword: {
required: true
},
},
messages
pswd: "Please enter password",
confirmpassword: "Please enter valid confirm password",
},
submitHandler: function() { $('#registration').submit(); }
});
});
both function $(document).ready(function() { // code here });
and $(function() { // code here });
are equivalent. Don't forget to code for server side validation!!!
两者的功能$(document).ready(function() { // code here });
和$(function() { // code here });
是等价的。不要忘记为服务器端验证编码!!!