Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Apr 2003
    Location
    Seoul, Korea
    Posts
    329
    Thanks
    0
    Thanked 0 Times in 0 Posts

    2 forms which has 3 input boxes per each in alerting

    Code:
    <script type = 'text/javascript'>
    
    function validate(f) {
    
    if (f['title1'].value.length == 0) {
    alert('Title1, please');
    return false 
    }
    
    if (f['message1'].value.length == 0) {
    alert('Message1, please');
    return false
    }
    
    if (f['password1'].value.length == 0) {
    alert('password1, please');
    return false
    }
    
    return true 
    }
    </script>
    
    <form action='action.cfm' method='post' onsubmit='return validate(this)'>
    
      <input name='title1' size='70'><br> 
    
      <textarea name='message1'
                  cols='70'
                  rows='10'></textarea><br>
    
      <input name='password1' size='70'><br> 
      
      <input type='submit' value='submit'> 
    
    </form>

    The above code has one form and it works fine in alert.
    I like to make it work in 2 forms. so I made the following code.


    Code:
    <script type = 'text/javascript'>
    
    function validate(f) {
    
    if (f['title1'].value.length == 0) {
    alert('Title1, please');
    return false 
    }
    
    if (f['message1'].value.length == 0) {
    alert('Message1, please');
    return false
    }
    
    if (f['password1'].value.length == 0) {
    alert('password1, please');
    return false
    }
    
    return true 
    }
    </script>
    
    <form action='action.cfm' method='post' onsubmit='return validate(this)'>
    
      <input name='title1' size='70'><br> 
    
      <textarea name='message1'
                  cols='70'
                  rows='10'></textarea><br>
    
      <input name='password1' size='70'><br> 
      
      <input type='submit' value='submit'> 
    
    </form>
    
    
    <script type = 'text/javascript'>
    
    function validate(f) {
    
    if (f['title2'].value.length == 0) {
    alert('Title2, please');
    return false 
    }
    
    if (f['message2'].value.length == 0) {
    alert('Message2, please');
    return false
    }
    
    if (f['password2'].value.length == 0) {
    alert('password2, please');
    return false
    }
    
    return true 
    }
    
    </script>
    
    
    <form action='action.cfm' method='post' onsubmit='return validate(this)'>
    
    <input name='title2' size='70'><br> 
    
    <textarea name='message2'
                  cols='70'
                  rows='10'></textarea><br>
    
    <input name='password2' size='70'><br> 
      
    <input type='submit' value='submit'> 
    
    </form>
    The above trial code which has two javascripts and 2 forms doesn't work correctly.

    The second form works fine in alerting but the first form doesn't work correctly in alerting?

    How can I make it work both the first form and the second form in alerting?
    Get my greedy up

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    You have two functions with the same name validate().

    I suggest you rename them validate1() and validate2().

    Hence :- onsubmit='return validate1(this)'

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by Philip M
    I suggest you rename them validate1() and validate2().
    But you can make a generic function that works for both form.

    Code:
    function validate(f, suffix) {
      if (f['title'+suffix].value.length == 0) {
        alert('Title'+suffix+', please');
        return false;
      }
      if (f['message'+suffix].value.length == 0) {
       alert('Message'+suffix+', please');
       return false;
      }
      if (f['password'+suffix].value.length == 0) {
       alert('Password'+suffix+', please');
       return false;
      }
      return true;
    }
    ...
    <form action="action.cfm" method="post" onsubmit="return validate(this, 1)">
    ...
    <form action="action.cfm" method="post" onsubmit="return validate(this, 2)">
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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