4 / 4
Apr 2010

Hello. I am trying to use javascript to validate a form, in order to require that my form variants I've set up are filled out, and that a license agreement checkbox is checked before the OnClick of the Add to Cart button is processed.



So there are two functions that the onClick event needs to call. How do I combine functions into the onClick event?



I currently have onClick="return agreed(this);" and want to add onClick="return validate_form ( );" so that it performs both functions. I've tried onClick="return agreed(this);return validate_form ( );" to no luck. Is there a way to combine the onClick functions?



I am using javascript code from http://www.elated.com/articles/form-validation-with-javascript/ to validate the form, and code from http://juxtaprose.com/articles/e-junkie-click-agree.html to validate the agreement checkbox. Both would seem to be called through the onClick event.



Or is there another way to require form variants that I do not know of? Thanks for the help, it is much appreciated!

  • created

    Apr '10
  • last reply

    Apr '10
  • 3

    replies

  • 1.1k

    views

  • 2

    users

  • 2

    links

Nix that. I solved it by just combining the function into one big one:



function validate_form ( )

{

valid = true;



if ( document.form_name.os0.value == "" )

{

alert ( "Please fill in the Name." );

valid = false;

}



if ( document.form_name.os1.value == "" )

{

alert ( "Please fill in the Address." );

valid = false;

}



if ( document.form_name.id_agree.checked==false )

{

alert ( "You must agree to our terms and conditions before proceeding with purchase." );

valid = false;

}



return valid;





}



and then just calling that on the onClick. You have to name the form, and id the checkbox and use those same names in the function in the appropriate spots.

Glad you got it sorted out. FWIW, one of our clients, CommonCraft.com, has done some pretty fancy stuff with our cart and javascript license agreements, so might be worth contacting them to see if they'd be willing to share tips and techniques.

Actually, the people from juxtaprose in the link above, where I got the agreement code, were the same people who made the CommonCraft website. They've gotten so many requests for the code, that they posted a little tutorial on it. I had written in to eJunkie and they also recommended CommonCraft -so I did a little Google searching and found the site. I'd definitely recommend the tutorial, it's really great!



In the end though, since I needed 3 validations and not just the one for the agreement, I did have to combine them all, into what you see above.