Reputation: 343
I really have no idea why my custom rule in jquery isnt working. all it is is looking for a substring in a text, making it required if it DOESNT appear, not required if it does. I am using the jquery validate library. I hope its something simple... please help!
jQuery.noConflict();
(function($) {
$(function() {
$("#frmUpdateCC").validate({
errorContainer: "#updateProfileCC",
errorPlacement: function(error, element) {},
rules: {
txtCardNo1: {
required: true,
creditcard: function() {
var str = $('#txtCardNo1').val();
str = (str.indexOf("xxxxxx"));
return (str != 6 );
}
}
cboMonth1: "required",
cboCard1: "required",
}
});
});
})(jQuery);
Ive tried using !== 6, !='6', and other variations thereof
Upvotes: 0
Views: 994
Reputation: 6446
You have to do like this
creditcard: function() {
var str = $('#txtCardNo1').val();
str = (str.indexOf("xxxxxx"));
if (str > -1) {
return true;
}
else {
return false;
}
}
i think the rule credit card will only support number format like nnn-nnn-nnn. So i think you have to add another rule according to your need, i mean custom rule .
Upvotes: 0
Reputation: 76
Well, str.indexOf("xxxxxx")
is going to return the start position of "xxxxxx"
in your string str
.
So, if str
contains 'xxxxxx'
, your statements would only evaluate as true if 'xxxxxx'
begins as position 6. Did you mean to test for str.length maybe?
Otherwise, why not simply do:
str = (str.indexOf("xxxxxx"));
return (str === -1 );
Upvotes: 2
Reputation: 12015
I think you want your callback function to be assigned to the "required" property, not the "creditcard" property:
required: function() { var str = .... }
Currently you have the required property hard-coded to true.
Upvotes: 0