Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

jQuery Validate

I use validatejs to validate form fields
http://jqueryvalidation.org/

The plugin uses field names to validate.  I need to use the field ID's but it does not seem to be working.  Can anyone tell me what I am doing wrong?

<script>
$().ready(function(){
	$("#update_cart_form").validate({ rules: {
			"#purchase_price": {
				digits: true
			},
			"#accessories_value": {
				digits:true,
				max:5000
			},
			
			"#contact_email": {
				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: {
			"#purchase_price": {
				digits: "Only a numeric value allowed"
			},
			
			"#accessories_value": {
				digits: "Only a numeric value allowed",
				max: "You can not have over $5000 in accessories"
			},
			"#contact_email": { 
				email: "You must supply a valid contact email address"
			}
		},
		errorElement: "div"
	});
		
});
</script>

Open in new window

Avatar of Tom Beck
Tom Beck
Flag of United States of America image

validate.js would not recognize "#" as an id identifier.

You can use any attribute identifier you need by enclosing it in quotes like this:
$().ready(function(){
	$("#update_cart_form").validate({ rules: {
			"input[id='purchase_price']": {
				digits: true
			},
			"input[id='accessories_value']": {
				digits:true,
				max:5000
			},
			
			"input[id='contact_email']": {
				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})\\]?$)"
			},

			
		},

Open in new window

Avatar of Robert Granlund

ASKER

For some reaso, that did not work.  I'm sure I have missed something obvious in the code.  I'll go over it again, thoroughly and if it still does not work, I'll re-post my updated code..
Sorry, don't knock yourself out. I was mistaken. All validate fields must have a unique name attribute. You can however use any selector if you use the rules add method.

http://jqueryvalidation.org/rules#.22add.22rules
I wrote it but It does not seem to work correctly.

$().ready(function(){
	$("#update_cart_form").validate({ rules: {
			$( "#purchase_price" ).rules( "add", {
					required: true,
					digits:true,
					messages: {
					required: "Please enter a purchase price",
					digits: "please enter numbers only"
					}
			}
					});
		errorElement: "div"
	});	

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial