Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.