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);

Open in new window

LVL 7
rgranlundAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bitaseme MboeDeveloperCommented:
In your code you have the following:

$(".submit_to_cart").on("click", validateCart0);
.
.
.
.
$(".submit_to_cart").on("click", validateCart1);

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.
0
rgranlundAuthor Commented:
Can you use an ID and a class on one button to activate both functions?
0
Bitaseme MboeDeveloperCommented:
You can...but it would be a better choice to just use binding to call a chain of events/functions. Have a look at binding in the jQuery API here.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgranlundAuthor Commented:
@bitaseme

This did not work.  Can you tell me what I have done wrong?
$(".submit_to_cart").bind("click", "validateCart1 validateCart2 ");
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.