...

View Full Version : validating a form



coutts
08-06-2008, 06:28 PM
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

<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>


<form action="insert.php" method="post" name="addform" onsubmit="return checkForm();">

binaryWeapon
08-06-2008, 09:42 PM
Doesn't Number() require an object (as in new Boolean() or something) as the parameter? You could try parseFloat() or parseInt() instead.

rangana
08-07-2008, 06:51 AM
Yes, Number() function requires an object, and the code above already has an object (highlighted):


Number(document.addform.pub_percent1.value);


I believe that one of the problem is this part (highlighted missing):


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() (http://www.w3schools.com/jsref/jsref_isNaN.asp) function in coordinate with the script you have.

You might find this modification useful:


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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum