• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1013
  • Last Modified:

jquery validation from anchor

I have searched through the documentation for jquery validation (http://docs.jquery.com/Plugins/Validation), but although I am sure what I need exists, I just cant find it.

I have a form, validating with jquery validation.  All good.  But I can't submit the form from a submit button (<input type=submit...).  I need to submit the form from an anchor (<a ... ), with the following javascript to submit the form (document.contact.submit();).

The problem is that submitting the form this way doesn't fire the validation, and I can't work out how to fire off the validation script programatically.

So, my question is.. How do I execute jquery validation programatically?

Thanks in advance for all help.

Jason
0
Jay1607
Asked:
Jay1607
  • 3
  • 3
1 Solution
 
richardneililaganCommented:
Hi,

you can just package the whole process in a function. :)
so, instead of calling submit() directly, you can just call your function - which would perform the validation, AND call submit() if it passes validation.

something like :
0
 
richardneililaganCommented:
haha, my code didn't attach to that, sorry!

here's a sample :
function validationProcess()
{
  // do stuff here
  // return TRUE or FALSE
}
 
function submitProcess()
{
    if (validationProcess()) { document.content.submit(); }
}
 
// then, for your anchor
<a id="submitAnchor">SUBMIT</a>
 
<script type="text/javascript">
$('a#submitAnchor').click(function(){ submitProcess(); });
</script>

Open in new window

0
 
Jay1607Author Commented:
richard...

Thank you.  I see what you are saying, and it makes sense.  I might sound really dumb here, but I can't see where my validation code fits into this.. Obviously it goes in the validatonProces() function, but I can't just drop it in..

how does this fit into your example...
$("#contact").validate({
			errorElement: "span",
			rules: {
				name: {
					required: true
				},
				email: {
					required: true,
					email: true
				},
				phone: {
					required: true
				},
				company: {
					required: true
					
				}
			},
			
			messages: {
				name: {
					required: ""
				},
				email: {
					required: "",
					email: ""
				},
				phone: {
					required: ""
				},
				company: {
					required: ""
				}
			},
			submitHandler: function(form) {
				$(form).ajaxSubmit( {
					success: function(){
						$("#Msg-formSent").slideDown();
						$(form).resetForm();
					}
				});
			}

		});

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
richardneililaganCommented:
ah, so you're actually using the the jQuery validate() function. :)

one of the things that .validate() does is to sets a flag which tells your code whether the form is valid or not. you can check this using the .valid() function :)

so your validationProcess() function would look similar to the ff:

(code snippet)

or something to that effect. :)

and, I always tell people I help with jQuery, complete your selectors.
so instead of $('#contact') , complete it to $('form#contact'). jQuery works a tad bit faster that way. ;)
function validationProcess()
{
  $('form#contact').validate(); // <-- substitute your validation
                                //     function above here. :p
  
  if ( $('form#contact').valid() ) { $('form#contact').submit() }
}

Open in new window

0
 
Jay1607Author Commented:
Thank you richardneilligan.  Your suggestion on the selectors is a great one.. makes sense.. thank you.

I have implemented your suggestion, which I can see should work, but I am still pulling my hair out trying to get it working.  I am sure it is something small and trivial.

I keep getting "validationProcess is not defined".  But it is.. I can see it there as plain as day.

I have put the site form and script online at http://d468378.u133.fasthit.net/Contact/ and would greatly appreciate if you could take a look and let me know what I am doing wrong.

Thank you again!!

Jason
0
 
Jay1607Author Commented:
Thank you richardneilligan.  Your suggestion on the selectors is a great one.. makes sense.. thank you.

I have implemented your suggestion, which I can see should work, but I am still pulling my hair out trying to get it working.  I am sure it is something small and trivial.

I keep getting "validationProcess is not defined".  But it is.. I can see it there as plain as day.

I have put the site form and script online at http://d468378.u133.fasthit.net/Contact/ and would greatly appreciate if you could take a look and let me know what I am doing wrong.

Thank you again!!

Jason
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now