SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question dropdown validation

    Hi

    I am trying to get an error message to display when nothing is selected in 3 textboxes on my form. I have managed to get things working for the text fields but cannot get my head around the drop downs.

    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    Edited Code:


    <head>
    <script language="JavaScript" type="text/JavaScript">

    function validEmail(email) {
    invalidChars = " /:,;"

    if (email == "") {
    return false
    }
    for (i=0; i<invalidChars.length; i++) {
    badChar = invalidChars.charAt(i)
    if (email.indexOf(badChar,0) > -1) {
    return false
    }
    }
    atPos = email.indexOf("@",1)
    if (atPos == -1) {
    return false
    }
    if (email.indexOf("@",atPos+1) > -1) {
    return false
    }
    periodPos = email.indexOf(".",atPos)
    if (periodPos == -1) {
    return false
    }
    if (periodPos+3 > email.length) {
    return false
    }
    return true
    }


    function checkOut(hire) {


    if (!validEmail(hire.email.value)) {
    alert("Please enter a valid e-mail address.")
    hire.email.focus()
    hire.email.select()
    return false
    }

    if (hire.name.value == "") {
    alert("Please enter your name in the space provided")
    hire.name.select()
    return false
    }

    if (hire.pickuptime.value == "") {
    alert("Please enter a pickup time in the space provided")
    hire.pickuptime.select()
    return false
    }

    return true
    }
    </script>
    <!-- End script -->
    </head>

    ******************************************************

    <body>

    <form name="hire" id="hire" method="post" action="hire2.php" onsubmit="return checkOut(hire)">

    <tr>
    <td width="216" height="26" class="labelcell"> <label for="name">Name
    * </label> </td>
    <td colspan="4" class="fieldcell"> <input type="text" name="name" id="name" tabindex="1"></td>
    </tr>

    <tr>
    <td width="216" height="26" class="labelcell"> <label for="email">E mail address
    * </label> </td>
    <td colspan="4" class="fieldcell"> <input type="text" name="email" id="email" tabindex="1"></td>
    </tr>

    <tr>
    <td height="26" class="labelcell"> <label for="pickuptime">Pickup
    Time *</label> </td>
    <td width="183" valign="top" class="fieldcell"><input type="text" name="pickuptime" id="pickuptime" tabindex="13"></td>
    <td width="4" valign="top"class="fieldcell"><select name="ampm" id="ampm">
    <option value="am">am</option>
    <option value="pm">pm</option>
    <option>select</option>
    </select></td>
    </tr>
    <tr>
    <td height="32" class="labelcell">Would You Like to Be Added
    To Our Mailing List *</td>
    <td colspan="4" class="fieldcell"><select name="maillist" id="maillist">
    <option value="Yes">Yes</option>
    <option value="No">No</option>
    <option>select</option>

    </select></td>
    </tr>
    <tr>
    <td height="30" colspan="5" valign="top"><input class="button" type="submit" name="Submit" value="Submit" onclick="checkOut()" tabindex="21"></td>
    </tr>
    </table>
    </form></td>
    </tr>
    </table>
    </body>
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    Help certainly appreciated

    Regards
    Colin
    Last edited by ColinHughes; Jan 14, 2005 at 08:09.

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all, remove onclick="checkOut()" from submit button.
    You have already set up onsubmit handler in form tag, and this should read:

    onsubmit="return checkOut(this)"

    To check if anything is selected in dropdown inspect its "value" property:
    Code:
    // (inside checkOut)
    if (!hire.ampm.value.length)
        alert("nothing selected in ampm!");
    Off Topic:

    BTW: it would be more helpful for people who read your post if you think about proper code formatting (indentation, [ code ] tags etc).

  3. #3
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply

    So now I have this:

    Code:
                    if (!hire.ampm.value.length)
        alert("nothing selected in ampm!");
    I get the alert popup but as soon as I click OK the form sends rather than give the opportunity to fix the error?

    Colin

    Sorry about the code window, I did try last time but it did not work?
    Last edited by ColinHughes; Jan 14, 2005 at 09:25.


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
  •