www.webdeveloper.com
Results 1 to 3 of 3

Thread: Javascript multiple if statements help

  1. #1
    Join Date
    Apr 2012
    Posts
    4

    Javascript multiple if statements help

    The if statements in the following javascript are not processing correctly. Namely, it stops processing the if statements if the field addshield_yn is not shown in the form.

    I have a form as specified below, programmed in coldfusion:
    Code:
    <cfform action="cart.cfm" method="post" name="form_add">
    
    <cfif fee_shield neq "">
    <p><cfinput type="checkbox" name="addshield_yn" id="addshield_yn" value="Yes" />
      <b> Add Shield Pro 
    </cfif>
    <cfif fee_branding neq "">
    	<p><cfinput type="checkbox" id="addbranding_yn" name="addbranding_yn" value="Yes" onClick="showhidefield()"/><b>Add Custom Branding</b>
    </p>
    <div id="brandingdiv1" style="display:none;">Type your Custom Brand or Team Name Here:<br />
      <textarea rows="3" name="branding" id="branding" cols="26" wrap="virtual" 
    onkeyup="document.form_add.word_count.value=document.form_add.branding.value.length;" onkeypress="return imposeMaxLength(event, this, #maxchars#);"></textarea>
        <br />
        special characters limited to: comma, apostrophe, pound, dash, and period<p>
          Character Count:
          <input type="text" name="word_count" size="2"  disabled="disabled"></div>
        
        <cfif fee_branding2 neq "">    <cfinput type="checkbox" id="addbranding2_yn" name="addbranding2_yn" value="Yes" onClick="showhidefield2()" />
    <b>Add Individual names or messages for each shaft on this order - $#fee_branding2# per shaft</b>
        <div id="brandingdiv2" style="display:none;"><p>Type your List of names or messages here separated by commas (max 24 characters each):<br />
          <textarea rows="3" name="branding2" id="branding2" cols="26"></textarea><br />
        special characters limited to: comma, apostrophe, pound, dash, and period</p></div>
        </cfif>
          </cfif>
          <p><b>Quantity of this exact shaft</b>: 
          <select name="addquantity">
    <cfloop from="#minqty#" to="#qtyavail#" index="i">
    <option value="#i#">#i#</option>
    </cfloop>
    </select></p>
    <cfinput type="submit" class="submit" name="addtocart"  value="Add To Cart" onClick="return validatechars()">
    <cfelse> 
    Out of Stock
    </cfif>
    </cfform>
    Javascript on the page is here:
    Code:
    <script language="javascript" type="text/javascript">
    function showhidefield()
    {
    var eSelect = document.getElementById('addbranding_yn');
    var optOtherReason = document.getElementById('brandingdiv1');
    if(eSelect.checked) {
    	optOtherReason.style.display = 'block';
    } else {
    	optOtherReason.style.display = 'none';
    	addbranding2_yn.checked = false;
    }
    }
    
    function showhidefield2()
    {
    var eSelect = document.getElementById('addbranding2_yn');
    var optOtherReason = document.getElementById('brandingdiv2');
    if(eSelect.checked) {
    	optOtherReason.style.display = 'block';
    	addbranding_yn.checked = true;
    	brandingdiv1.style.display = 'block';
    } else {
    	optOtherReason.style.display = 'none';
    }
    }
    
    function imposeMaxLength(Event, Object, MaxLen)
    {
    	document.form_add.addbranding_yn.checked = true;
        return (Object.value.length <= MaxLen)||(Event.keyCode == 8 ||Event.keyCode==46||(Event.keyCode>=35&&Event.keyCode<=40))
    }
    </script>
    
    function validatechars() {
    if(document.form_add.addbranding_yn.checked == true && document.form_add.addshield_yn.checked == true) {
    	alert("You cannot select both custom branding and shield pro on this shaft. \nPlease remove one and try again.");
        return false;}
    	
    	if(document.form_add.addbranding_yn.checked == true && document.form_add.branding.value.length < 1) {
    	alert("You selected branding but did not type in your custom brand. \nPlease either unselect branding or type in your custom brand and try again.");
        return false; }
    	
    if(document.form_add.addbranding2_yn.checked == true && document.form_add.branding2.value.length < 1) {
    	alert("You selected individual branding but did not type in your custom brand. \nPlease either unselect individual branding or type in your custom brand and try again.");
        return false;}
    	
    var iChars = "!@$%^&*()+=[]\\\;/{}|\":<>?";	
    
    if (document.form_add.branding.value.length > 0) {
      for (var i = 0; i < document.form_add.branding.value.length; i++) {
        if (iChars.indexOf(document.form_add.branding.value.charAt(i)) != -1) {
        alert("Your custom brand has invalid special characters. \nPlease remove them and try again.");
        return false;
        }
      }
    }
    
    if (document.form_add.branding2.value.length > 0) {
    	  for (var i = 0; i < document.form_add.branding2.value.length; i++) {
        if (iChars.indexOf(document.form_add.branding2.value.charAt(i)) != -1) {
        alert("Your team list has invalid special characters. \nPlease remove them and try again.");
        return false;
        }
      }
    }
    
    }

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,653
    at least, the function validatechars() goes beyond the <script language="javascript" type="text/javascript"></script> tags, how can it work?!

    as for me, i don't know some tags from the html you gave so it is hard to test the js-part without rewritting the page markup

  3. #3
    Join Date
    Apr 2012
    Posts
    4
    Thanks for your reply. The examples can be seen here:


    Link: http://www.hickorylax.com/detail.cfm?pid=1
    Note: The addbranding2_yn checkbox is not defined on this product
    Situation: Click both boxes (shield pro and branding) and try to submit the form, it correctly throws an error saying you can't do that. However, try to enter illegal characters (!, / etc) and it won't throw an alert as it is supposed to. It is not processing the for loop to validate special characters. If statements in the function validatechars() are not processed.

    Link: http://www.hickorylax.com/detail.cfm?pid=28
    Note: The addshield_yn checkbox is not shown or defined on this product
    Situation: Click the first branding box and type in illegal characters and it won't throw an error. Click the checkboxes and leave textboxes blank and it wont throw an error (it is supposed to). If statements in the function validatechars() are not processed.

    Your help is appreciated.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles