WEB form validation

Discussion in 'OT Technology' started by JEWbacca, Jan 9, 2009.

  1. JEWbacca

    JEWbacca =/

    Joined:
    May 10, 2001
    Messages:
    2,230
    Likes Received:
    0
    Location:
    Long Beach, CA
    I'm trying to use a java form validation but have run into an issue. The form has two different submit buttons serving different purposes and only one which needs to be validated, but because i'm using onsubmit="return validate(this)" it validates all buttons which types are set to submit.

    Is there a work around to this? :hs:
     
  2. 95vr4

    95vr4 OT Supporter

    Joined:
    Oct 6, 2004
    Messages:
    2,513
    Likes Received:
    0
    Location:
    Weddington, NC
    Post the code

    There shouldn't be 2 submits on 1 form (although there can be more than one button).

    Also theres a difference between java and javascript, this is going to be javascript
     
  3. JEWbacca

    JEWbacca =/

    Joined:
    May 10, 2001
    Messages:
    2,230
    Likes Received:
    0
    Location:
    Long Beach, CA
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script type="text/javascript">
    function validate(form) {
    
    if(!document.compose.finalize.checked){alert("Please select the 'Finalize' box and press 'Submit' to release your answer.");
    return false; }
    
    return true;
    }
    </script>
    
    
    </head>
    
    <body>
    
    <form id="compose" name="compose" method="post" action="compose.php?action=save" onsubmit="return validate(this)">
      Title:<br />
      <input name="title" type="text" id="title" class="forms" size="40" maxlength="100"/>
      <br />
      <br />
    Body:<br />
    <textarea name="response" cols="52" rows="20" wrap="virtual" class="forms"> </textarea>
    <br />
    <br />
    	<table width="100%" border="0" cellpadding="0">
      <tr>
        <td><div align="left"><input type="submit" name="submit" id="submit" class="forms" value="Save" /></div></td>
        <td><div align="center">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; or</td>
        <td><div align="right"><input type="checkbox" name="finalize" id="finalize" value="yes" /> 
      Finalize and <input type="submit" name="submit" id="submit" class="forms" value="Submit" /></div></td>
      </tr>
    </table>
    </form>
    </body>
    </html>
    
     
  4. 95vr4

    95vr4 OT Supporter

    Joined:
    Oct 6, 2004
    Messages:
    2,513
    Likes Received:
    0
    Location:
    Weddington, NC
    well I guess 2 submits may work if you gave them different names, you could request the name on the processing page ($buttonclicked = $_POST['nameofbutton'];). They're both submitting the form though which should call the validation. Is it not working for both buttons?
     
  5. JEWbacca

    JEWbacca =/

    Joined:
    May 10, 2001
    Messages:
    2,230
    Likes Received:
    0
    Location:
    Long Beach, CA
    It is working for both buttons, my goal is for it NOT to work for both buttons but instead only one of them.
     
  6. JEWbacca

    JEWbacca =/

    Joined:
    May 10, 2001
    Messages:
    2,230
    Likes Received:
    0
    Location:
    Long Beach, CA
    The form works in a similar way as composing an email.

    Users can type into the form and can either save (draft) or submit (send). Both of these buttons are submit buttons as they save data to the database, but only the SUBMIT button needs to be validated as I don't care if the form does not validate while they are still composing a draft.

    Any ideas for this would be EXTREMELY useful. Thanks.
     
  7. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,915
    Likes Received:
    8
    Location:
    Los Angeles
    You should both validate on the server side and client side. Client side validation for the JavaScript validation you speak of, and server side validation for security purposes.
     
  8. JEWbacca

    JEWbacca =/

    Joined:
    May 10, 2001
    Messages:
    2,230
    Likes Received:
    0
    Location:
    Long Beach, CA
    Okay. Agreed. But can you spread some insight on how to only validate one of the submit buttons....
     
  9. Karnejj

    Karnejj “A true conservative is one who can't see any diff OT Supporter

    Joined:
    Jan 9, 2008
    Messages:
    35,585
    Likes Received:
    0
    Location:
    UPGRADED USA Shutdown Today:8 derps
    Perhaps you can change your design from two submit buttons over to something like 2 radio buttons and ONE submit button.

    In the validate function, you can then verify which radio button is active and either validate or not validate as you appropriate to your wishes.
     
  10. fishbulb

    fishbulb Active Member

    Joined:
    Oct 29, 2001
    Messages:
    6,848
    Likes Received:
    4
    Location:
    md
    remove the onSubmit, add this to the button you want to validate with:
    Code:
     onClick="return validate();"
    and change function validate(form) to function validate()
    You don't need to pass the whole form to the JS function since you're referencing the form directly in the JS code, not the form passed to the function.
     
  11. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,915
    Likes Received:
    8
    Location:
    Los Angeles
    This will work. Apply onClick validation on both the save and submit buttons. Although, this method you are going about using is kind of chinsy. I'd recommend using a framework. jQuery is a good one. There's a jQuery form plugin for validation as well (to ts).
     

Share This Page