SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    Free me php klassicd's Avatar
    Join Date
    Sep 2001
    Location
    San Diego
    Posts
    509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    such a strict language

    hey everone. would someone mind looking at the code and perhaps telling me what i might be doing wrong?

    Code:
     <script language="JavaScript" type="text/JavaScript">
    function checkForm()
     {
     var is_checked = document.forms["submitscore"].forfeit.checked;
     if (is_checked == false)
       var r1h = parseFloat(document.forms["submitscore"].r1h.value);
       var r2h = parseFloat(document.forms["submitscore"].r2h.value);
       var r1a = parseFloat(document.forms["submitscore"].r1a.value);
       var r2a = parseFloat(document.forms["submitscore"].r2a.value);
       
       var totalHome = parseFloat(r1h + r2h);
       var totalAway = parseFloat(r1a + r2a);
       var totalScore = totalHome + totalAway;
       if (totalScore != 24)
    	{
    	 alert('The Round 1 and Round 2 scores do not add up to an even 24 rounds.');
    	 return false;
    	}
       return true;
      }
      return true;
     }
    </script>

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, offhand what I see you're missing is an opening brace after your first if statement. However, you can make your code quite a bit more efficient by storing the form as a reference, a refactoring some of your variables out of existence.
    Code:
    function checkForm()
    {
    	var f = document.forms["submitscore"];
    	if ( !f.forfeit.checked )
    	{
    		var r1h = parseFloat( f.r1h.value );
    		var r2h = parseFloat( f.r2h.value );
    		var r1a = parseFloat( f.r1a.value );
    		var r2a = parseFloat( f.r2a.value );
    		var totalScore = r1h + r2h + r1a + r2a; // already numbers, no need to run parseFloat on total
    
    		if ( totalScore != 24 )
    		{
    			alert('The Round 1 and Round 2 scores do not add up to an even 24 rounds.');
    			return false;
    		}
    	}
    	return true;
    }
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    Free me php klassicd's Avatar
    Join Date
    Sep 2001
    Location
    San Diego
    Posts
    509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your help.

  4. #4
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If you want good error reporting, install NS. The latest Netscape has superb error reporting, and will point you to the exact problem at the line itself (creates a link to problem line). I use it all the time, to check my site and to do ALL error reporting. It also allows the embedding of WMP objects, with all controls. which is cool.....
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if


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
  •