SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Error message: Can't have 'break' outside of loop

    Hi all
    I get this message: Can't have 'break' outside of loop with this code to validate that the fields have been entered.
    How do I get rid of this message? Thank you

    Code:
    <script type="text/javascript">
      function validate_affil(){
      	var v =  true;
      		$('.validate_affil').each(function(){
      			if( $.trim($(this).val()) == '' ){
      				alert('The fields marked (*) are required')
      				v = false;
      				break;
      			}
      		});
      	return v;
      }
    </script>
    
    <div class="SignupForm" class="width:100%;">
    <form onsubmit='return validate_affil();' action="signup.php" method="post" style="border:0px solid;color:black;" id="icpsignup8855">
    
    
    <fieldset style="border:0px;">
    <label>Email*<input type="text" name="username" size="10" class='validate_affil' ></label>
    <label>Name*<input type="text" name="firstname" size="10"  class='validate_affil' ></label>
    <label>Surname*<input type="text" name="lastname" size="10"  class='validate_affil' ></label>
    	   <input type="hidden" name="parentuserid" id="parentuserid" value="" /> </fieldset> 
    	  <div id="FormMessage"></div>
    	   <input type="image" name="submit" value="Register" src="image/register.png" width="124" height="25" alt="Send" />
            
    	</div>
    </form>
    
    <script id="pap_x2s6df8d" src="salejs.php" type="text/javascript"></script>
                              <script type="text/javascript">
                                PostTracker.writeAffiliateToCustomField('parentuserid');
                              </script>
    
    </div>
    </div>

  2. #2
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    instead of break use return false.
    Bye.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,682
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    To explain further, the jQuery each() command allows you to return false to break the loop, whereas anything else that's returned results in the equivalent of continue.

    From jQuery.each()
    We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Paul and wisher learnt a new thing today! Works great


Bookmarks

Posting Permissions

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