Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    validating a form

    Hello:
    I was wondering what I have done wrong below. The form always validates and proceeds to insert.php. I have even put alert('Hello'); at the begining of teh javascript and I dont ever see that either so I am assuming that the javascript function isnt even being called. I have tried adding onClic to the submit button as well - same results HELP PLEASE
    Thanks
    Robert
    Code:
    <script language="javascript" type="text/javascript">
    function checkForm(){
    
          var pub_percent1=Number(document.addform.pub_percent1.value);
          var pub_percent2=Number(document.addform.pub_percent2.value);
          var pub_percent3=Number(document.addform.pub_percent3.value);
          var pub_percent4=Number(document.addform.pub_percent4.value);
    
          var song_percent1=Number(document.addform.song_percent1.value);
          var song_percent2=Number(document.addform.song_percent2.value);
          var song_percent3=Number(document.addform.song_percent3.value);
          var song_percent4=Number(document.addform.song_percent4.value);
    
          var song_total=song_percent1+song_percent2+song_percent3+song_percent4;
          var pub_total=pub_percent1+pub_percent2+pub_percent3+pub_percent4;
          
    
    
    
       if(pub_total != 100) || (song_total != 100)
       {
          alert('Your Royalties do not add up to 100% - Please try again');
          return false;
       }
    
    }
    </script>
    Code:
    <form action="insert.php" method="post" name="addform" onsubmit="return checkForm();">

  • #2
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts
    Doesn't Number() require an object (as in new Boolean() or something) as the parameter? You could try parseFloat() or parseInt() instead.

  • #3
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Yes, Number() function requires an object, and the code above already has an object (highlighted):
    Code:
    Number(document.addform.pub_percent1.value);
    I believe that one of the problem is this part (highlighted missing):
    Code:
    if((pub_total != 100) || (song_total != 100))
    Also, you haven't had an exception if for values that is not a number, you might find it useful to use isNaN() function in coordinate with the script you have.

    You might find this modification useful:
    Code:
    function checkForm(){
    var flag=true;
    		var pub_percent1=Number(document.addform.pub_percent1.value);
          var pub_percent2=Number(document.addform.pub_percent2.value);
          var pub_percent3=Number(document.addform.pub_percent3.value);
          var pub_percent4=Number(document.addform.pub_percent4.value);
    
          var song_percent1=Number(document.addform.song_percent1.value);
          var song_percent2=Number(document.addform.song_percent2.value);
          var song_percent3=Number(document.addform.song_percent3.value);
          var song_percent4=Number(document.addform.song_percent4.value);
    
          var song_total=song_percent1+song_percent2+song_percent3+song_percent4;
          var pub_total=pub_percent1+pub_percent2+pub_percent3+pub_percent4;
    	if(isNaN(song_total)||isNaN(pub_total)){
    	alert('Please key in a number.');flag=false;}
      else if(pub_total != 100||song_total != 100)
       {alert('Your Royalties do not add up to 100&#37; - Please try again');
        flag=false}
    return flag;
    }
    </script>
    Last edited by rangana; 08-07-2008 at 05:55 AM. Reason: Provide an alternative
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph


  •  

    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
    •