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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2003
    Location
    Northern Ireland
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form validation - required fields

    Hi. I've got a form that I want to validate so that all fields are required. The code for the page is very long so I've reproduced snippets below:

    This is in the Head section:

    <script language="javascript" type="text/javascript">

    <!-- hide from older browsers -->

    function validForm(Form) {
    if (Form.email.value =="")
    {
    alert("You must enter an e-mail address")
    Form.email.focus()
    return false
    }
    if (Form.email.value !=Form.email2.value)
    {
    alert("Entered e-mail addresses did not match")
    Form.email.focus()
    Form.email.select()
    return false
    }

    if (Form.firstname.value =="")
    {
    alert("Please tell us your First Name")
    Form.firstname.focus()
    Form.firstname.select()
    return false
    }
    if (Form.lastname.value =="")
    {
    alert("Please tell us your Last Name")
    Form.lastname.focus()
    Form.lastname.select()
    return false
    }
    countryChoice = Form.Country.selectedIndex
    if (Form.Country.options[countryChoice].value == "")
    {
    alert("Please tell us where you live")
    return false
    }
    return true
    }

    // end hiding script -->
    //-->
    </script>

    The form I am using is:

    <form onSubmit="return validForm(this)" action="<?php echo $editFormAction; ?>" method="POST" name="frmSubscribe" id="frmSubscribe">
    <p> <span class="sub-heading-1">Sign up for Mailing List:</span><br />
    </p>
    <table width="444" border="0" align="center">
    <tr>
    <td width="150" class="sub-heading">E-mail Address:</td>
    <td width="240"><input type="text" name="email" size="40"/>
    </td>
    </tr>
    <tr>
    <td class="sub-heading">Confirm e-mail address:</td>
    <td><label>
    <input name="email2" type="text" id="email2" size="40">
    </label></td>
    </tr>
    <tr>
    <td class="sub-heading">First Name:</td>
    <td><input name="firstname" type="text" /></td>
    </tr>
    <tr>
    <td class="sub-heading">Last Name:</td>
    <td><input type="text" name="lastname" /></td>
    </tr>
    <tr>
    <td class="sub-heading">Country:</td>
    <td><select name="Country" id="Country" style="width:185px" >
    <option value= "" selected="selected">-- choose your Country --</option>
    <option value="Afghanistan">Afghanistan</option>
    <option>Albania</option>
    -- too many countries to post here --
    <option>Yugoslavia</option>
    <option>Zambia</option>
    <option>Zimbabwe</option>
    </select></td>
    </tr>
    <tr>
    <td> </td>
    <td> </td>
    </tr>
    <tr>
    <td> </td>
    <td><input name="Subscribe" type="submit" id="Subscribe"
    value="Subscribe" /></td>
    </tr>
    <tr>
    <td> </td>
    <td> </td>
    </tr>
    </table>


    <input type="hidden" name="MM_insert" value="frmSubscribe">
    </form>

    What I am getting is that the Alert box works fine prompting the user to enter the required info until you get to the Country field. If you leave it blank it gives the alert and when you choose a country it still gives the alert. It seems to have got stuck and won't let me submit the form at all.

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I only see that your first two options actually have values being declared....

    <option value= "" selected="selected">-- choose your Country --</option>
    <option value="Afghanistan">Afghanistan</option>
    <option>Albania</option>

    In the above example, Albania has no value so of course the value will remain empty and your validation function will fail...

    .....Willy

  • #3
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Aw heck....
    Your question was already answered here....

    Have a nice day;
    .....Willy

  • #4
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    08-12-2004 04:58 AM: I only see that your first two options actually have values being declared....


    08-12-2004 02:16 PM: Thanks, but I seem to have fixed it myself.


    Last edited by jamescover; 08-12-2004 at 04:12 PM.
    "God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "


  •  

    Posting Permissions

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