We help IT Professionals succeed at work.
Get Started

jQuery Validation

Robert Granlund
on
89 Views
Last Modified: 2015-04-22
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

Comment
Watch Question
Developer
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE