Foxy Forum Status

We're no longer responding to questions via our forum, but we will keep it up for historical reasons. If you can't find the answer you're looking for, please visit our knowledge base or contact us. If there's enough interest in the future, we may bring the forum back.

My Validation Code

ChuckTrukkChuckTrukk Member
in Bugs & Feature Requests edited January 2008
Just wanted to post my validation code here. Took me a while to figure out. If anyone wants to modify/improve it, please feel free. Just want to help anyone else who needs it.
<script language="javascript">

	// are these needed?
	var size = "";
	var color = "";

//run this from the jquery function below
function fc_PreProcess(color, size) {

	//clear error_string
	var	error_string = "";
		if (color == "") 	{		error_string = error_string + "You must select a color.\n";		}
		if (size  == "") 	{		error_string = error_string + "You must select a size.\n";		}
		if (size  == "" || color == "") {
			return false; 
		} else {
			return true;

$j(function() {

	//run validation on forms with submit (if some products dont need this validation, you can give them a different class name)
	$j('.submit').bind('click', function() {
			//first drop-down item
			var size = $j(this).parents("form:first").find("select:eq(0)").val();
			//second drop down item
			var color = $j(this).parents("form:first").find("select:eq(1)").val();
			//run fc_PreProcess
			fc_PreProcess(color, size);
			if (size  == "" || color == "") {
				//do not submit the form
				return false;

PS - is there a way to use pre tags in this forum so the indents will stay?
  • this is what i was looking for, but after installing in my modx template i get the following error from firebug (and it does not work):

    missing } after function body

    any help appreciated! chas
  • brettbrett FoxyCart Team
    The problem is you have:
    $j('.submit').bind('click', function() {
    But you don't close that function anywhere, so there's an unclosed function.

    I'd also try replacing $j(function() { with $j(document).ready(function(){
  • thanks for the quick response brett! . . . but, you are talking to a designer here *s* found this code that chuck graciously shared with the forum, but i have no clue on how to close the function - suggestion?

  • brettbrett FoxyCart Team
    Ha, ok.
    Add another }); above the last }); you have in your code.

    Actually, in looking at your code... I don't think that'll work even if the javascript is correct ;)

    You want to validate a form before sending, right? You kind of have it backwards. You want your validation script inside fc_PreProcess(), not the other way around. I'm going to assume that's a bit over your head though, so we'll need to help, yes? ;)

    Where'd you find that code? If we know what we're trying to achieve it'll help us help you.
  • thanks for the feedback . . . this code was posted on your forum and i added my call for help to the original post with the code.

    i would like to validate size/color input to make sure customers make those choices before sending to the cart.

    *lol* yes, just a "bit" over my head!

    any assistance would be appreciated.

  • brettbrett FoxyCart Team
    Hey chas.
    Can you whisper me the URL that you're trying to get this going on? This question is being asked a bunch so I'm going to try to start some wiki stuff on basic validation, but real life examples work best.
  • brettbrett FoxyCart Team
    Hey Chas.
    Heads up that I'll be playing around with this over the weekend. Sorry for the delay.
  • no problem. am most appreciative of your kind assistance!
  • brettbrett FoxyCart Team
    Sorry this is super late in coming. I wrote up a big long tutorial, then quickly closed it without saving (cuz, you know, I'm just rad like that), and couldn't motivate myself to rewrite it. But I finally managed to rewrite most of it, so here goes:

    Let me know if that helps, or if you need additional help. If you do need additional help, let me know in which areas. That'd be very helpful.
  • EBEB Member
    edited May 2009
    Hi Brett,
    This is a great wiki, so glad it is there! But I have a custom application where I have a maximum quantity of 5 flavor options. Example: if you buy the Sampler pack you can choose up to 4 flavors from 5 flavor options. Can I just add each flavor field to the "if" and require them to equal 4 total, like this?
    <script type="text/javascript" charset="utf-8">
    	function fc_PreProcess() { // First let's define the function
    		if ($('#cherries|#berries|#toffee|#cocoa|#mocha').val() != '4') {
    			// The .val() returns the value of the chosen element
    			// Our condition is !=, which means "not equal to"
    			return true; // So if the value of the text input isn't blank, we'll return true and add the item to the cart
    		} else { // Otherwise...
    			// Let's alert the user to the error:
    			alert('You must select only 4 flavors for this item.');
    			// Then return false:
    			return false; // We return false, the cart is not displayed, and the product is not added.

    Sorry this should probably be moved to basic category.

  • brettbrett FoxyCart Team
    Hi Bill.
    I think you have some syntax errors with the multiple pipes, should be commas for jQuery I think, but I could be wrong on what you're trying to do.

    Do you have an HTML page you're working with? That'd be helpful I think to understand what you're after.
  • Hello Brett, I am working with Bill to resolve this issue. We just need to limit the quantity amount entered.

    I will send you the link to the site privately as we dont want google to pick up the site just yet before we launch.


  • THis forum doesnt seem to allow private message so I added a link to a page with a no follow on it.
  • brettbrett FoxyCart Team
    Hi. First off, check the console in Firebug. Looks like you're using FoxEE with FoxyCart v051. FoxyCart v051 changed the jQuery stuff a little bit, so you'll probably need to use this little extra bit of code:

    As far as private messaging goes, it's called a "whisper" in this forum (Vanilla). It's kind of neat once you get the hang of it.

    As far as limiting products go, do you have code you're working off of? The basic idea is pretty straightforward, but it'd be easier to work off an attempt at it. I'd do something like:
    var cherries = (jQuery('#cherries').val()) ? parseInt(jQuery('#cherries').val()) : 0;
    var berries = (jQuery('#berries').val()) ? parseInt(jQuery('#berries').val()) : 0;
    var toffee = (jQuery('#toffee').val()) ? parseInt(jQuery('#toffee').val()) : 0;
    var cocoa = (jQuery('#cocoa').val()) ? parseInt(jQuery('#cocoa').val()) : 0;
    var mocha = (jQuery('#mocha').val()) ? parseInt(jQuery('#mocha').val()) : 0;
    if ((cherries + berries + toffee + cocoa + mocha) != '4') {

    You don't have IDs assigned to your inputs, so you'd have to use input[name=cherries] instead of #cherries, but hopefully you can get it going.
  • EBEB Member
    Thanks Brett, I installed the jquery nonconflict mode. Will whisper some thoughts to you on the code.
Sign In or Register to comment.