Required Fields?

aptdesignaptdesign Member
in Help edited December 2008
Hi guys. I have a few different options for each of my products offered, selected through select drop-downs. Is it outside of the realm of FoxyCart to force those to be required values?

If so, do you have any suggestions on how to make those required before you can move on to checkout?

Thanks!
Comments
  • You might want to try some javascript form validation to make a required field:

    http://letmegooglethatforyou.com/?q=javascript+required+fields

    I too am wondering if is an alternative.
  • brettbrett FoxyCart Team
    Is this what you're looking for:
    http://wiki.foxycart.com/docs:jquery:basic_validation
    ?

    If you're talking about an add-to-cart form, then that wiki page should be very helpful. If you're talking about checkout validation that's a different wiki page:
    http://wiki.foxycart.com/docs:checkout:customfields#required_fields
  • Wow, you guys are awesome. Sorry I couldn't find that earlier when looking around for required field info. Its just what I needed.
  • Hi,

    I'm starting up this cart after almost a year of it being turned off. Things are working out well except for this validation. The code is not stopping someone who has not made a selection in each field.

    I followed the link to the "Basic Validation" again and it some of the code seems to have changed from last year?

    Here's what I used last year that worked:
    <script type="text/javascript" charset="utf-8">
    	function fc_PreProcess() {
    		var errors = 0; // Let's set a variable called 'errors', and set the value to 0.
    		if (!$j('select[@name=workshop1]').val()) {
    			errors ++; // This is shorthand for "add 1 to errors".
    		}
    		if (!$j('select[@name=workshop2]').val()) {
    			errors ++; // This is shorthand for "add 1 to errors".
    		}
    		if (!$j('select[@name=workshop3]').val()) {
    			errors ++; // This is shorthand for "add 1 to errors".
    		}
    		if (!$j('select[@name=workshop4]').val()) {
    			errors ++; // This is shorthand for "add 1 to errors".
    		}
    		if (errors > 0) {
    			alert('Please complete all the required fields');
    			return false;
    		} else {
    			return true;
    		}
    	}
    </script>
    

    Now in the wiki it shows this
    if (!$('select[$name=size]').val()) {
    

    instead of this
    if (!$j('select[@name=workshop1]').val()) {
    

    I made those changes but they don't help the validation and break the Cart's lightbox.

    Sorry to be a nuisance, but does anyone know what I'm doing wrong or what might have changed since last year?

    Thanks in advance for your help!
  • lukeluke FoxyCart Team
    Hey aptdesign, welcome back. :)

    A few things have changed, most notably, jQuery. We upgraded it so you'll want to remove all the "@" in the selectors and we're no longer calling jQuery in no-conflict mode so you should replace $j with jQuery in all that code. Once that's all been done and if you're still having problems, shoot us a URL and we'll take a look.

    On a side note, we're planning on versioning the documentation in the future to avoid these confusions. So much to do. :)
  • That worked like a charm. Thank you Luke!
  • Hi again,

    Back for my yearly updates to my FoxyCart and required fields are not working again. Didn't change anything in the validation code that worked last year and can't find any new updates for required fields in v.0.7.0

    Here's what I'm using:
    <input name="submit" value="Add to Cart" class="submit" type="submit" />
      
        <script type="text/javascript" charset="utf-8">
    	function fc_PreProcess() {
    		var errors = 0; // Let's set a variable called 'errors', and set the value to 0.
    		if (!jQuery('select[name=workshop1]').val()) {
    			errors ++; // This is shorthand for "add 1 to errors".
    		}
    		if (!jQuery('select[name=workshop2]').val()) {
    			errors ++; // This is shorthand for "add 1 to errors".
    		}
    		if (!jQuery('select[name=workshop3]').val()) {
    			errors ++; // This is shorthand for "add 1 to errors".
    		}
    		if (!jQuery('select[name=workshop4]').val()) {
    			errors ++; // This is shorthand for "add 1 to errors".
    		}
    		if (errors > 0) {
    			alert('Please complete all the required fields');
    			return false;
    		} else {
    			return true;
    		}
    	}
    </script>
    

    Any magical fixes again this year?

    Thanks!
  • lukeluke FoxyCart Team
    Did you add the "Please Select One" text to the select boxes since last year? The code you have there is looking for no value at all but since that value is there, that's what gets passed to the cart. Your option is setup as
    <option>Please Select One</option>
    
    but it probably should be something like
    <option value="">Please Select One</option>
    
    and then your validation if checks should probably check for an empty string:
    if (jQuery('select[name=workshop2]').val() == "") {
    

    Hope that helps. If not, just post back and we'll take a closer look.
  • Aha. Thanks for pointing that out Luke. I did have those fields last year, but we upgraded this year to Joomla 1.5 and the WYSIWYG editor was stripping out
    value=""
    
    when I was pasting the code from Coda.

    That's the fix! Thank you sir!
Sign In or Register to comment.