1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help Debugging Javascript Validation!

Discussion in 'JavaScript' started by jb77, Oct 26, 2006.

  1. #1
    Can anybody help me (a javascript newbie) figure out why this script validation isn't working? :confused: I'm tearing my hair out with this... any help is appreciated!!

    <HTML>
    <HEAD>
    <TITLE>Survey</TITLE>
    
    <script type="text/javascript" language="JavaScript">
    <!--
    function CheckRequiredFields() {
    var errormessage = new String();
    	
    if(validate(document.Survey.x_Hobbies))
    	{ errormessage += "\n\nPlease answer what your \"other\" hobbies are (question #3)."; }
    	
    if(errormessage.length > 2) {
    	alert('NOTE:' + errormessage);
    	return false;
    	}
    return true;
    }
    
    function validate(ss) {
    if ((ss.Other==true)&&(document.Survey.x_Hobbies_Other.value.length==0)) { return true; }
    return false;
    }
    
    //-->
    </script>
    
    <link rel="StyleSheet" type="text/css" href="store.css" media="screen">
    </HEAD>
    <BODY>
    <table width="100%" cellpadding="20" cellspacing="0" bgcolor="#FFFFFF" border="0">
    	<tr>
    		<td>
    		<h1 class="survey">Survey</h1>
    		<form action="/cgi-bin/MasterFeedback.cgi" method="post" name="Survey" onSubmit="return CheckRequiredFields()">
    		<input type="hidden" name="subject" value="Shopping Survey-0610">
    		<input type="hidden" name="realname" value="john">
    		<input type="hidden" name="email" value="john@doe.com">
    		<input type="hidden" name="thankyou" value="/cgi-bin/survey0610_confirm.cgi">
    		<ol class="questions">		 
    			<li>What are your favorite hobbies? <span class="note">(choose all that apply)</span>
    				<ul class="cols">
    					<li><input type="checkbox" name="x_Hobbies" value="Music" /> Music</li>
    					<li><input type="checkbox" name="x_Hobbies" value="Sports" /> Sports</li>
    					<li><input type="checkbox" name="x_Hobbies" value="ArtsCrafts" /> Arts / Crafts</li>
    					<li><input type="checkbox" name="x_Hobbies" value="Reading" /> Reading</li>
    					<li><input type="checkbox" name="x_Hobbies" value="TV" /> Watching TV</li>
    					<li><input type="checkbox" name="x_Hobbies" value="Cooking" /> Cooking</li>
    					<li><input type="checkbox" name="x_Hobbies" value="HomeDecorating" /> Home Decorating</li>
    					<li><input type="checkbox" name="x_Hobbies" value="Fashion" /> Fashion</li>
    					<li><input type="checkbox" name="x_Hobbies" value="Shopping" /> Shopping</li>
    					<li class="widecol"><input type="checkbox" name="x_Hobbies" value="Other" /> Other: <input type="text" name="x_Hobbies_Other" size="40" maxlength="" onfocus="this.className='currentField'" onchange="this.className='changedField'" /></li>
    				</ul>
    			</li>	
    		</ol>
    		 
    		<p>Thank you for filling out the survey.  Please click SUBMIT!</p>
    		
    		<p class="center"><input type="image" src="images/2006/btn_submit_wht.gif" name="submitbutton" alt="Submit" /><input type="reset" value="Clear Form" class="resetbtn" alt="Clear All Form Fields" onclick="if (!confirm('Are you sure you want to clear this form?')) return false;" /></p><br />
    		</form>
    		</td>
    	</tr>
    </table>
    
    </BODY>
    </HTML>
    
    Code (markup):
     
    jb77, Oct 26, 2006 IP
  2. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #2
     
    Logic Ali, Oct 26, 2006 IP
  3. jb77

    jb77 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the reply Logic Ali, I still can't seem to get it to work...

    I tried 10-15 combinations:
    if (ss[ss.length-1].checked)
    if (ss[ss.length-10].checked)
    if (ss[10].checked)

    none of which are working, and I can't give it a unique name for other reasons. So how do you write the checkbox position correctly?
     
    jb77, Oct 26, 2006 IP
  4. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #4
    function validate(ss)
    {
     return ss[ss.length-1].checked && document.Survey.x_Hobbies_Other.value.length==0 ;
    }
    Code (markup):
     
    Logic Ali, Oct 26, 2006 IP