SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 29
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    form validation HELP!!!

    i ha ve the form below

    <form name="form1" action="post_processing.php?<? echo SID ?>" method="post" >
    <tr> <td align="center">Category : <select name="age" >
    <option value="none">
    <option value="child">under 12
    <option value="teen">
    <option value="adult">20-64
    <option value="senior">65 or Older</select></td></tr>
    <tr> <td align="center">
    Title <br>
    <input type"text" name="title" size="100"> </td></tr> <tr> <td align="center" valign="middle">
    <table align="center" valign="baseline" width=100% height=100%> <tr> <td align="center" valign="middle">
    Text Here<br><textarea name="text" rows="20" cols="90" wrap="hard"></textarea></td></tr></table>
    </td></tr>
    <tr><td align="center">Image Upload <input type="file" name="file"> </td></tr> <tr> <td align="center" valign="baseline">
    <br><input type="submit" value="Send" >
    <input type="reset" value="Clear"></td></tr>
    </form>


    I wanna validate the all the fields because nothing must be empty or null, Could you help me?

  2. #2
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the form
    <form onSubmit="ValidateForm(this);">

    In the header

    <script>
    function ValidateForm(Form)
    {
    var Element1 = documentGetElementById("First");
    // Get all elements of the form

    if(Element1.value != "" && Element" != "") /* Check all elements here, depending on the element type will determine the check required. */
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    </script>

    This is the simplist way, but for a more generic approch you might want to iterate through the form.elements collection testing each one. In this case you'd need to check the type of the element and apply the correct check.
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because i have no idea concerning javascript could you give more particular details about the form i have posted?

  4. #4
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First give each element an id attribute i.e. id="elem1" or something like that.

    Second:

    var Element1 = documentGetElementById("Elem1");
    var Element2 = documentGetElementById("Elem2");
    var Element3 = documentGetElementById("Elem3");
    var Element4 = documentGetElementById("Elem4");

    if(Element1.selectIndex != 0 && Element1.value != "" && Element3.value != "" && Element4.value != "")
    {
    return true;
    }
    else
    {
    return false
    }

    **********

    PS = input type="text", you missed the =

    And if you find something doesn't work about this then put in a alert("interesting info") in the function so that you can investigate what is going on.
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i put the script below

    <script>
    function ValidateForm(Form) {

    var Element1 = documentGetElementById("Elem1");
    var Element2 = documentGetElementById("Elem2");
    var Element3 = documentGetElementById("Elem3");

    if(Element1.selectIndex != 0 && Element1.value != "" && Element2.value != "" && Element3.value != "")
    {
    return true;
    }
    else
    {
    return false;alert("interesting info");
    }

    }
    </sctipt>

    and onSubmit="ValidateForm(this);" int the form tag

    but now nothing is being shown to me, actually an empty page.

    What is wrong?

    I would like to mention that the file upload input must be out of validation because it is optional for someone to upload a file.

  6. #6
    SitePoint Enthusiast
    Join Date
    Dec 2002
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it's </script> not </sctipt>
    -Mark

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have written my own script and is like this below

    <script language="javascript">
    function check () {
    category=document.form1.category.value;
    title=document.form1.title.value;
    text=document.form1.text.value;
    if (!(category&&title&&text)) alert("You must fill the fields");return false;}
    </script>
    ....
    ....
    .
    ....


    <input type=submit value=send name=submit onClick="check();">

    But when i click on the button the alert activated as i expexted but when i press Ok the progress go on and the php file is being executed,

    Could you tell me my fault?

  8. #8
    SitePoint Enthusiast
    Join Date
    Dec 2002
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah. get rid of the onSubmit, and replace the field type "submit" with the field type "button" and put onClick="check()" and in javascript, if it's not empty, then do document.form1.submit();
    -Mark

  9. #9
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but i want the submit to stop if any field is null. In my case the execution doesn't stop but goes on!
    Very odd!

  10. #10
    SitePoint Member
    Join Date
    Dec 2002
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've developed for my site very innovative javascript controls.
    try one of my forms to see how it works.
    http://www.unpicked.com
    $8.49 Domain name registrations,
    free site submission & more...
    http://buy@unpicked.com

  11. #11
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I would appreciate if you give me the script in the Head tag and the form so i could understand Is that possible?

  12. #12
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a working script. Note I left the debug alert statements in - I was leaving something for you to do!

    PHP Code:
    <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
    <
    html>
    <
    head>
    <
    title> New Document </title>
    <
    meta name="Generator" content="EditPlus">
    <
    meta name="Author" content="">
    <
    meta name="Keywords" content="">
    <
    meta name="Description" content="">
    <
    script>
    function 
    ValidateForm()
    {
        var 
    Element1 document.getElementById("elem1");
        var 
    Element2 document.getElementById("elem2");
        var 
    Element3 document.getElementById("elem3");
        var 
    Element4 document.getElementById("elem4");

    //    alert(Element1.tagName + " " +Element2.tagName+ " " +Element3.tagName);
    //    alert(Element1.selectedIndex + " " +Element2.value+ " " +Element3.value);
        
    if(Element1.selectedIndex != && Element2.value+"" != "" && Element3.value+"" != "")
        {
            return 
    true// allow submission
        
    }
        else
        {
            
    alert("All fields except the image are required.")
            return 
    false//deny submission
        
    }
    }
    </script>
    </head>

    <body>

    <form name="form1" action="jonsof.asp" method="post" onSubmit="return ValidateForm();">
    <table>
    <tr><td align="center">Category : <select id="elem1" name="age"><option value="none"><option value="child">under 12<option value="teen">teen<option value="adult">20-64<option value="senior">65 or Older</select></td></tr>
    <tr><td align="center">Title<br><input id="elem2" type="text" name="title" size="100"></td></tr>
    <tr><td align="center" valign="middle">
      <table align="center" valign="baseline" width="100%" height="100%">
      <tr><td align="center" valign="middle">Text Here<br><textarea id="elem3" name="text" rows="20" cols="90" wrap="hard"></textarea></td></tr></table>
    </td></tr>
    <tr><td align="center">Image Upload <input id="elem4" type="file" name="file"></td></tr>
    <tr><td align="center" valign="baseline"><br><input type="submit" value="Send"><input type="reset" value="Clear"></td></tr>
    </table>
    </form>

    </body>
    </html> 
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  13. #13
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It didn't work, not only was the page alerted when i pressed the button but also the form go on with the form processing, why that?

  14. #14
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Must be a bug somewhere. Worked perfectly on mine.

    Note I was posting back to itself, you need to change that yo your post file.

    1) What browser are you using?

    2) Since you are using PHP can you capture (view source) the HTML that arrives at the browser and post that.

    Cheers,
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  15. #15
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have IE 5 and i can seen the html which arrives from a php file!


    The script i posted to you in a previous post of mine worked as i expected but it didn't prevent the form processing just showes an alrt message and then the process goes on.

  16. #16
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I meant can you post the HTML on this forum so I can see it.

    The way onSubmit works is that it should not submit the form if the function returns false - this has worked fine everytime I have used it so something else must be affecting thigns.
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  17. #17
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-7">
    <meta name="Generator" content="Dev-PHP 1.3.0">
    <title>Document Title</title>
    <script language="javascript">
    <script>
    function ValidateForm()
    {
        var Element1 = document.getElementById("elem1");
        var Element2 = document.getElementById("elem2");
        var Element3 = document.getElementById("elem3");
        var Element4 = document.getElementById("elem4");

    //    alert(Element1.tagName + " " +Element2.tagName+ " " +Element3.tagName);
    //    alert(Element1.selectedIndex + " " +Element2.value+ " " +Element3.value);
        if(Element1.selectedIndex != 0 && Element2.value+"" != "" && Element3.value+"" != "")
        {
            return true; // allow submission
        }
        else
        {
            alert("All fields except the image are required.")
            return false; //deny submission
        }
    }
    </script>

    </head>
    <body bgcolor=aqua>
    <center><a href=user_place.php>Go to the User Area</a></center>
    <table align="center" valign="baseline" height=100% width=100% border="1">
    <caption> User Interface </caption>
    <tr> <td colspan="2" align="center"><?echo $user_element['username'];?></td> </tr>
    <form name="form1" action="post_processing.php?<? echo SID ?>" method="post" enctype="multipart/form-data" >
    <tr> <td colspan="2" align="center">Category : <select name="category" >
    <option value="">Επιλέξτε μία κατηγορία</option>
    <option value="">----------------------</option>
    <? mysql_connect("localhost");mysql_select_db(db_name);
    $result1=mysql_query("SELECT categoryid, category FROM categories");
     while(
    $row=mysql_fetch_assoc($result1)) {
     echo 
    "<option value=".$row['categoryid'].">".$row['category']."</option>";}
    ?></select></td></tr>
    <tr> <td colspan="2" align="center">
    Title <br>
    <input type"text" name="title" size="100"> </td></tr> <tr> <td colspan="2" align="center" valign="middle">
    <table align="center" valign="baseline" width=100% height=100%> <tr> <td colspan="3" align="center" valign="middle">
    Text Here<br><textarea name="text" rows="20" cols="90" wrap="hard"></textarea></td></tr></table>
    </td></tr>
    <tr><td colspan="2" align="center">Φωτογραφία <input type="file" name="file"> </td></tr>
    <tr><td height="2" align="center"><input type=submit name=send value="Αποστολή" onClick="return ValidateForm();"></td><td height="2" align="center"><input type=reset value="  Clear "></td></tr>
    </form>
    </table>

    </body>
    </html>

  18. #18
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats the PHP, not the HTML posted to the browser. I want to see the exact HTML that arrives at the browser - do you have a test site? Otherwise can you post it here. Cheers.
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  19. #19
    SitePoint Enthusiast
    Join Date
    Dec 2002
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...if you was to do as I told you in that last post...


    here...

    Code:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-7">
    <meta name="Generator" content="Dev-PHP 1.3.0">
    <title>Document Title</title>
    <script language="javascript">
    <script>
    function ValidateForm()
    {
        var Element1 = document.getElementById("elem1");
        var Element2 = document.getElementById("elem2");
        var Element3 = document.getElementById("elem3");
        var Element4 = document.getElementById("elem4");
    
    //    alert(Element1.tagName + " " +Element2.tagName+ " " +Element3.tagName);
    //    alert(Element1.selectedIndex + " " +Element2.value+ " " +Element3.value);
        if(Element1.selectedIndex != 0 && Element2.value+"" != "" && Element3.value+"" != "")
        {
            document.form1.submit();
        }
        else
        {
            alert("All fields except the image are required.");
        }
    }
    </script>
    
    </head>
    <body bgcolor=aqua>
    <center><a href=user_place.php>Go to the User Area</a></center>
    <table align="center" valign="baseline" height=100% width=100% border="1">
    <caption> User Interface </caption>
    <tr> <td colspan="2" align="center"><?echo $user_element['username'];?></td> </tr>
    <form name="form1" action="'post_processing.php?<? echo SID ?>" method="post" enctype="multipart/form-data" >
    <tr> <td colspan="2" align="center">Category : <select name="category" >
    <option value="">Επιλέξτε μία κατηγορία</option>
    <option value="">----------------------</option>
    <? mysql_connect("localhost");mysql_select_db(db_name);
    $result1=mysql_query("SELECT categoryid, category FROM categories");
     while($row=mysql_fetch_assoc($result1)) {
     echo "<option value=".$row['categoryid'].">".$row['category']."</option>";}
    ?></select></td></tr>
    <tr> <td colspan="2" align="center">
    Title <br>
    <input type"text" name="title" size="100"> </td></tr> <tr> <td colspan="2" align="center" valign="middle">
    <table align="center" valign="baseline" width=100% height=100%> <tr> <td colspan="3" align="center" valign="middle">
    Text Here<br><textarea name="text" rows="20" cols="90" wrap="hard"></textarea></td></tr></table>
    </td></tr>
    <tr><td colspan="2" align="center">Φωτογραφία <input type="file" name="file"> </td></tr>
    <tr><td height="2" align="center"><input type=button name=send value="Αποστολή" onClick="ValidateForm();"></td><td height="2" align="center"><input type=reset value="  Clear "></td></tr>
    </form>
    </table>
    
    </body>
    </html>
    -Mark

  20. #20
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still doesn't explain why my solution doesn't work - they are both essentually the same.
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  21. #21
    SitePoint Enthusiast
    Join Date
    Dec 2002
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by dale_burrell
    Still doesn't explain why my solution doesn't work - they are both essentually the same.
    Yeah, I've always had the same problem he's having trying to do form validation the way you gave, so I just started doing it the way I showed.
    -Mark

  22. #22
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats strange, cos I can't reproduce that behaviour and have always had it work OK? Are you using an old browser?
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  23. #23
    SitePoint Enthusiast
    Join Date
    Dec 2002
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    naw, my primary browsr is IE6, and I also use Mozilla 1.0.1.

    No clue why I have the problem, it's weird.
    -Mark

  24. #24
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you finf any solution?

  25. #25
    SitePoint Enthusiast
    Join Date
    Dec 2002
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by jonsof
    Did you finf any solution?

    yes...what I gave you...
    -Mark


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
  •