0

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

3 Answers 3

2

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 );
Sign up to request clarification or add additional context in comments.

3 Comments

I'll give that a try... Basically I need the statement to evaluate as false If that statement exists ( in the sixth position or any position ).... I've tried -1 but I'll give it another go.
that should work. If the string is not found, then indexOf would be equal to -1...so, str === -1 would return true. If the "xxxxxx" is found, then the indexOf will be greater than -1, so str === -1 would return false.
You are most welcome. Sorry I hadn't gotten back to you before now.
0

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.

1 Comment

Nope I need it to only check for the credit card property If those Xs aren't there. It's always required.
0

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 .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.