Jump to content

Javascript validation


mckeegan375

Recommended Posts

Hi All!

 

I was hoping someone could help me with some simple JS form validation which involves a loop.

 

So far, i have the following:

 

<SCRIPT language=JavaScript>

function validateForm()

{

 

var a = document.getElementsById('totalpayment');

var b = document.getElementsById('netpay');

 

for(var k=0;k<b.length;k++){

var c = a[k]

var d = b[k]

 

if (c.value > d.value)

{

alert("Total payment cannot be greater than net pay");

return false;

}

}

 

}

</script> 

 

 

My form has a loop which pulls data from a mysql database and it contains the fields 'totalpayment' and 'netpay' yet the validation does not display the alert if the total payment entered is greater than the net pay.

 

Any help would be much appreciated.

 

Cheers

Andy

Link to comment
https://forums.phpfreaks.com/topic/258188-javascript-validation/
Share on other sites

 var a = document.getElementsById('totalpayment'); //it's getElementById, notice the lack of s
var b = document.getElementsById('netpay'); 

 

This is invalid. You cannot grab multiple elements by ID. What you need to use is getElementsByClassName and attach the relevant class to the elements you wish to check.

 

Hope that helps you,

Joe

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.