使用 SSN 正则表达式、屏蔽输入和依赖项进行 Jquery 验证
我无法让 Jquery 验证符合以下规则。
我用 SSN 格式屏蔽了该字段,验证不得允许在没有完整 SSN 的情况下提交此信息。我有一个有效的正则表达式函数,可以拒绝非 SSN。我也需要它不允许空白提交(仅当字段处于焦点状态时才应用掩码)。最后但并非最不重要的一点是,如果选中另一个复选框 (#noSSN),则不需要该字段。
我添加了一个 JSfiddle 页面来提供帮助: http://jsfiddle.net/B2UpW/3/
它在小提琴页面上的工作似乎有点不同。希望这有帮助!
编辑:我还没有收到任何回复..好奇我的问题是否有任何困惑?欢迎提出任何有助于寻求帮助的建议!
$("#ssn").mask("999-99-9999");
$.validator.addMethod("notEqual", function(value, element, param) {
return this.optional(element) || value !== param;
}, "Please choose a value!");
$("#applicantForm").validate({
rules: {
ssn: {
notEqual: "___-__-____",
required: {
depends: function(element) {
return ($("#ssn").val() == ""
|| isValidSSN($("#ssn").val())
|| $("#noSSN:unchecked"));
}
}
}
},
messages: {
ssn: 'Please enter a social security number.'
}
});
function isValidSSN(value) {
var re = /^([0-6]\d{2}|7[0-6]\d|77[0-2])([ \-]?)(\d{2})\2(\d{4})$/;
if (!re.test(value)) { return false; }
var temp = value;
if (value.indexOf("-") != -1) { temp = (value.split("-")).join(""); }
if (value.indexOf(" ") != -1) { temp = (value.split(" ")).join(""); }
if (temp.substring(0, 3) == "000") { return false; }
if (temp.substring(3, 5) == "00") { return false; }
if (temp.substring(5, 9) == "0000") { return false; }
return true;
}
I'm having trouble getting the Jquery Validation to work with the following rules.
I have the field masked with SSN format, the validation must not allow submission of this information without it being a complete SSN. I have a working regex function that rejects non SSNs. I need it to not allow blank submissions as well (the mask is only applied if the field is onFocus). And last but not least, the field is NOT required if another checkbox is checked (#noSSN).
I've added a JSfiddle page to help: http://jsfiddle.net/B2UpW/3/
It appears to be working a bit different on the fiddle page. Hope this helps!
Edit: I've yet to receive any responses.. curious if there is any confusion about my question? Any suggestions to help find some assistance is welcome!
$("#ssn").mask("999-99-9999");
$.validator.addMethod("notEqual", function(value, element, param) {
return this.optional(element) || value !== param;
}, "Please choose a value!");
$("#applicantForm").validate({
rules: {
ssn: {
notEqual: "___-__-____",
required: {
depends: function(element) {
return ($("#ssn").val() == ""
|| isValidSSN($("#ssn").val())
|| $("#noSSN:unchecked"));
}
}
}
},
messages: {
ssn: 'Please enter a social security number.'
}
});
function isValidSSN(value) {
var re = /^([0-6]\d{2}|7[0-6]\d|77[0-2])([ \-]?)(\d{2})\2(\d{4})$/;
if (!re.test(value)) { return false; }
var temp = value;
if (value.indexOf("-") != -1) { temp = (value.split("-")).join(""); }
if (value.indexOf(" ") != -1) { temp = (value.split(" ")).join(""); }
if (temp.substring(0, 3) == "000") { return false; }
if (temp.substring(3, 5) == "00") { return false; }
if (temp.substring(5, 9) == "0000") { return false; }
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
网站所有者,请停止使用上述 jammypeach 或 Stephen S. 的代码。一些有效的 SSN 被其正则表达式拒绝。
请参阅http://www.socialsecurity.gov/employer/randomization.html
自 2011 年 6 月(该线程创建之前)以来,
可以以“773”及以上开头。请将
([0-6]\d{2}|7[0-6]\d|77[0-2])
替换为\d{3}
- 现在因为你我必须亲自去银行;-)Website owners, please stop using the piece of code from jammypeach or Stephen S. above. Some valid SSNs are rejected by their regexp.
See http://www.socialsecurity.gov/employer/randomization.html
SSNs can start with "773" and above since June 2011, before that thread was created.
Please replace
([0-6]\d{2}|7[0-6]\d|77[0-2])
with\d{3}
- now I have to go to the bank in person because of you ;-)您只需要添加一个检查来查看该复选框是否被选中。
请参阅这个更新的 fiddle,大约第 9 行(或下面代码中的第 2 行),您可以在其中检查是否SSN 有效:
如果选中该框,现在将允许提交,否则将要求正确的 SSN。
You just needed to add a check to see if the checkbox was, er, checked.
See this updated fiddle, around line 9 (or line 2 in the code below) where you check if the SSN is valid:
It will now allow a submit if the box is checked, otherwise it will demand a correct SSN.
目前还不太清楚到底是什么不起作用......
无论如何我看到了一些事情:
什么是
notEqual
?是你添加的方法吗? 中有一个名为pattern
的方法与插件一起提出的附加方法脚本。您的
depends
的目的是什么?添加规则required
来检查值不为空,并添加规则isValidSSN: true
来执行您自己的 SSN 验证。仅使用
depends
(或required: function(element) { return $("#noSSN").is(":unchecked"); }
)来未选中复选框时应用规则。希望这会有所帮助,d。
It's not very clear what is not working exactly...
I see a few things anyway:
What is
notEqual
? Is it a method you added ? There is an method calledpattern
in the additionnal methods script proposed along with the plugin.What is the purpose of your
depends
? Add a rulerequired
to check the value is not empty and add a rule likeisValidSSN: true
to execute your own SSN validation.Only use the
depends
(orrequired: function(element) { return $("#noSSN").is(":unchecked"); }
) to apply the rules when the checkbox is not checked.Hope this helps, d.