Ad

Adding In JQuery Validate

- 1 answer

I was watching a video tutorial on adding jQuery validation to contact forms, but none of it is working. They never really discussed how to add the validate plugin to the page, so I am thinking that may be where I am going wrong.

The jQuery call out he had at the top of his page was this:

<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>

Again, he didn't go into it more than to google jQuery validate and that it would show up, so I assumed that it was this:

http://jqueryvaltarget="_blank" rel="nofollow noreferrer" href="http://jqueryvalidation.org/" >http://jqueryvalidation.org/

Does anyone see anything wrong with what I did for it to not work? I got the actual form to work, just not the jQuery validate.

jQuery(document).ready(function($){
		$("#contactform").validate({
			rules: {
				name: {
					required: true;
					minlength: 2
				},
				email: {
					required: true;
					email: true;
				},
				message: {
					required: true;
					minlength: 5
				}
			},
			messages: {
				name: {
					required: "Please enter your name",
					minlength: "Your name seems a bit short, doesn't it?"
				},
				email: {
					required: "Please enter your email address",
					email: "Please enter a valid email address"
				},
				message: {
					required: "Please enter your message",
					minlength: "Your message seems a bit short. Please enter at least 5 characters"
				}
			}
		});
	});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<form id="contactform" method="post" action="" novalidate>

			<input type="text" name="name" placeholder="Name">
			<input type="email" name="email" placeholder="Email">
			<textarea name="message" placeholder="Message"></textarea>
			<input type="submit" name="submitform" value="Send">
		</form>

Ad

Answer

When you set values for object properties you should place , after values instead of ;. So change required: true; (three places), email: true; to required: true, and email: true,

Then your code will work, like here: fiddle

Ad
source: stackoverflow.com
Ad