Robert Granlund
asked on
jQuery Validation
I have a jQuery Validation script that uses validatejs.js. The code below works for the first instance of it but the second does not work. At a glance is there anything I am doing wrong?
<script>
var validateCart0 = function() {$("#update_cart_form_0").validate({ rules: {"item_options[0][first_name]": {required:true},
"item_options[0][last_name]": {required:true},
"item_options[0][association_membership]": {required:true},
"item_options[0][gender]": {required:true},
"item_options[0][purchase_price]": {
required:true,
digits:true
},
"item_options[0][accessories_value]": {
digits:true,
max:5000
},
"item_options[0][contact_email]": {
required: true,
email:true,
regex:"^((\"[\\w\\s-]+\")|([\\w-]+(?:\\.[\\w-]+)*)|(\"[\\w\\s-]+\")([\\w-]+(?:\\.[\\w-]+)*))(@((?:[\\w-]+\\.)*\\w[\\w-]{0,66})\\.([a-zA-Z]{2,6}(?:\\.[a-zA-Z]{2})?)$)|(@\\[?((25[0-5]\\.|2[0-4][0-9]\\.|1[0-9]{2}\\.|[0-9]{1,2}\\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\\]?$)"
}
},
messages: {"item_options[0][first_name]": {
required: "Required"
},
"item_options[0][last_name]": {
required: "Required"
},
"item_options[0][association_membership]": {
required: "Required"
},
"item_options[0][gender]": {
required: "Required"
},
"item_options[0][marital_status]": {
required: "Required"
},
"item_options[0][purchase_price]": {
required: "Required",
digits: "Only a numeric value allowed"
},
"item_options[0][accessories_value]": {
digits: "Only a numeric value allowed",
max: "Over $5000 in accessories not allowed"
},
"item_options[0][contact_email]": {
required: "Required",
email: "Please enter a valid email",
regex: "Incorrect format"
}
},
errorElement: "div"
});
};
$(".submit_to_cart").on("click", validateCart0);
var validateCart1 = function() {$("#update_cart_form_1").validate({ rules: {"item_options[1][first_name]": {required:true},
"item_options[1][last_name]": {required:true},
"item_options[0][association_membership]": {required:true},
"item_options[0][gender]": {required:true},
"item_options[1][purchase_price]": {
required:true,
digits:true
},
"item_options[1][accessories_value]": {
digits:true,
max:5000
},
"item_options[0][contact_email]": {
required: true,
email:true,
regex:"^((\"[\\w\\s-]+\")|([\\w-]+(?:\\.[\\w-]+)*)|(\"[\\w\\s-]+\")([\\w-]+(?:\\.[\\w-]+)*))(@((?:[\\w-]+\\.)*\\w[\\w-]{0,66})\\.([a-zA-Z]{2,6}(?:\\.[a-zA-Z]{2})?)$)|(@\\[?((25[0-5]\\.|2[0-4][0-9]\\.|1[0-9]{2}\\.|[0-9]{1,2}\\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\\]?$)"
}
},
messages: {"item_options[1][first_name]": {
required: "Required"
},
"item_options[1][last_name]": {
required: "Required"
},
"item_options[0][association_membership]": {
required: "Required"
},
"item_options[0][gender]": {
required: "Required"
},
"item_options[0][marital_status]": {
required: "Required"
},
"item_options[1][purchase_price]": {
required: "Required",
digits: "Only a numeric value allowed"
},
"item_options[1][accessories_value]": {
digits: "Only a numeric value allowed",
max: "Over $5000 in accessories not allowed"
},
"item_options[0][contact_email]": {
required: "Required",
email: "Please enter a valid email",
regex: "Incorrect format"
}
},
errorElement: "div"
});
};
$(".submit_to_cart").on("click", validateCart1);
ASKER
Can you use an ID and a class on one button to activate both functions?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@bitaseme
This did not work. Can you tell me what I have done wrong?
$(".submit_to_cart").bind( "click", "validateCart1 validateCart2 ");
This did not work. Can you tell me what I have done wrong?
$(".submit_to_cart").bind(
Open in new window
This is saying that whenever you click on an element belonging to the class ".submit_to_cart" then call validateCart0 AND validateCart1. jQuery sees both lines of code but is only calling the first. Are you trying to call both functions? If so then use separate classes. Usually though, during a click method, you would want to use an id selector (which is the '#' sign minus the quotes) instead of a class selector '.' (again minus the quotes).
You'll have to have two different Id's to whatever gui object you are clicking though. So if you have two buttons you can have id1 and id2 then use one to validate cart 0 and the other to validate cart 1.