Results 1 to 8 of 8

Thread: can't get validate to work

  1. #1
    Join Date
    May 2009
    Location
    Greensboro, GA
    Posts
    163
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default can't get validate to work

    I have a form with 3 phone numbers. One of the 3 is required. Why doesn't this work.
    My first line of the form is:

    Code:
    <form id="infoupdateform" method="post" action="xxx.php" onsubmit="validateform()">  
    
    
    <script type="text/javascript">
    function validateform(){
      if ((document.getElementByName('home').value == '') && (document.getElementByName('cell').value == '') && (document.getElementByName('work').value == '')) 
    	   {
             alert('At least 1 phone number is required');
             document.getElementById('home').focus();
         }
    }
    </script>
    Thanks for any help
    Last edited by keyboard; 06-18-2014 at 11:13 PM. Reason: Format: Code Tags [code][/code]

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    There could be other problems. But when validating via the onsubmit event, the return value is what determines whether or not the form submits (add the red highlighted, two places) and, when using document.getElementByName, the 0 based index of the element within the collection of all elements that might have that name must be supplied (add green highlighted, 3 places):

    Code:
    <form id="infoupdateform" method="post" action="xxx.php" onsubmit="return validateform();">  
    
    <script type="text/javascript">
    function validateform(){
      if ((document.getElementByName('home')[0].value == '') && (document.getElementByName('cell')[0].value == '') && (document.getElementByName('work')[0].value == '')) 
    	   {
             alert('At least 1 phone number is required');
             document.getElementById('home').focus();
             return false;
         }
    }
    </script>
    This assumes the form and page has three text input fields, 1 and only one (or at least the first of) each with a name of 'home', 'cell', and 'work' in the form.

    The browser cache may need to be cleared and/or the page refreshed to see changes.

    If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.


    Note: There can be more than one element on a page with a given name, that's why the index must be specified.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    May 2009
    Location
    Greensboro, GA
    Posts
    163
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    didn't work. I put in an alert and I got to the validate but not the "if".
    I've been reading about getelement online for 3 days and I get more confused each day. I've also tried 30 varieties of validate and none worked.

    My code is at www.lotatennis.com. Go to the very bottom left and click "testing". Enter Marty Colton, submit. All 3 phone numbers are blank. Hit submit and it should go to validate. It just goes back to the front page. BTW, I used home, cell, and work in the example above but they are really zhome, zcell, and zwork. The online code does match.

    Thanks for the help. I'll be gone for 3-4 days.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    It shows. There is no getElementByName, sorry I missed that. There is a getElementsByName. This code works:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    </head>
    <body>
    <form id="infoupdateform" method="post" action="xxx.php" onsubmit="return validateform();">
    Home: <input type="text" name="home">
    Cell: <input type="text" name="cell">
    Work: <input type="text" name="work">
    <input type="submit" value="Update">
    </form>
    
    <script type="text/javascript">
    function validateform(){
      if ((document.getElementsByName('home')[0].value == '') && (document.getElementsByName('cell')[0].value == '') && (document.getElementsByName('work')[0].value == '')) 
    	   {
             alert('At least 1 phone number is required');
             document.getElementsByName('home')[0].focus();
             return false;
         }
    }
    </script>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    May 2009
    Location
    Greensboro, GA
    Posts
    163
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    It worked.....partially. it got to the alert statement but the focus statement didn't work. After I hit ok, it returned to my home page.
    I also tried getElementsById and I also tried removing the return false statement.

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    May 2009
    Location
    Greensboro, GA
    Posts
    163
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    It makes a difference where the validate is. I had it before the form. It works if I put the validate after the form.
    Thanks
    Last edited by mcolton; 06-23-2014 at 04:45 PM.

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    In the example page I made, it doesn't matter where the function is. I cannot speak for your page, I haven't seen it. Code cannot execute before elements in the page for which it's looking as it executes have been parsed. And code cannot be assigned to elements that haven't been parsed at the time assignment is attempted. Neither of those cases apply to my simple example code and markup. Again, I cannot speak for the exact code and markup you're using. But it's also possible that it was some other problem that got corrected during the process of moving the code.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. The Following User Says Thank You to jscheuer1 For This Useful Post:

    mcolton (06-23-2014)

Similar Threads

  1. Help me Validate this
    By jmituzas in forum PHP
    Replies: 0
    Last Post: 08-14-2010, 02:48 AM
  2. validate JPG
    By lord22 in forum JavaScript
    Replies: 4
    Last Post: 11-07-2009, 09:54 PM
  3. Validate int
    By d-machine in forum PHP
    Replies: 1
    Last Post: 08-26-2009, 11:21 AM
  4. Resolved & won't validate
    By mcolton in forum JavaScript
    Replies: 3
    Last Post: 08-23-2009, 04:31 PM
  5. Validate This!
    By jscheuer1 in forum CSS
    Replies: 7
    Last Post: 05-20-2006, 06:02 PM

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
  •