Javascript input validation not working

I am trying to validate the billing email and phone number inputs on this page using a script that is already in place on the site:

Test Page

I have tried all sorts of variations, and can’t get it to work. Clicking submit with the email and phone fields empty triggers the “email required” message as intended. However, putting an email address into the field doesn’t stop the message from appearing, and the form won’t submit. Can anyone see what I’m doing wrong?

Hi there,

The problem with your script is this line:

if (document.getElementsByName('BillEmail').value == null)

which in your case is always undefined, regardless of whether you enter a value.

This is because document.getElementsByName will return a collection of HTML elements.

As you want to access the first element, change your code to:

if (document.getElementsByName('BillEmail')[0].value == '')

and you’ll be good to go.

Oh, and you’ll have to do the same for BillPhone, too.

Oh thank you, and thank you for taking the time to explain it in such a clear way. It makes perfect sense now that I see what it’s doing. Better yet, it’s working! Much appreciated :slight_smile:

My pleasure :slight_smile: