admin管理员组文章数量:1305255
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 plete 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: /
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 wele!
$("#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 plete 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/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 wele!
$("#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;
}
Share
Improve this question
edited Sep 21, 2014 at 2:24
Sparky
98.7k26 gold badges202 silver badges290 bronze badges
asked Oct 7, 2011 at 15:36
Stephen S.Stephen S.
8357 gold badges16 silver badges30 bronze badges
3 Answers
Reset to default 4Website 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
Previously unassigned area numbers were introduced for assignment
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 ;-)
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:
$('#submitApplication').live('click', function() {
if ($('#noneSSN').is(':checked'))
{
alert("SUCCESS");
}
else
{
var isValid = $("#applicantForm").valid();
if (isValid) {
alert("SUCCESS");
}
}
return false;
});
It will now allow a submit if the box is checked, otherwise it will demand a correct SSN.
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.
本文标签: javascriptJquery Validation with SSN RegexMasked Input and DependsStack Overflow
版权声明:本文标题:javascript - Jquery Validation with SSN Regex, Masked Input and Depends - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741790507a2397630.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论