We help IT Professionals succeed at work.

jQuery Validation

Robert Granlund
on
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

Bitaseme MboeDeveloper

Commented:
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.

Author

Commented:
Can you use an ID and a class on one button to activate both functions?
Developer
Commented:
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.

Author

Commented:
@bitaseme

This did not work.  Can you tell me what I have done wrong?
$(".submit_to_cart").bind("click", "validateCart1 validateCart2 ");