SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2009
    Location
    Arizona
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,002
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    Quote Originally Posted by TinyQueen View Post
    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?
    The problem with your script is this line:

    Code JavaScript:
    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:

    Code JavaScript:
    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.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2009
    Location
    Arizona
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,002
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    My pleasure


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •