SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2002
    Location
    Wales
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validate select boxes

    Hi All,

    I'm trying to validate two fields: A text field and a select. The test requires that both should not be empty. I've tried this so far:

    Code:
     
    function validate()
    {
    var myfm = document.forms["testform"];
    var addmodel = myfm.add_model.value;
    var model = myfm.modelID.selectedIndex.value;
     
    if(addmodel.length == 0 && model.length == -1){
    alert("You must fill either model field");
    return false;
    } else {
    return true;
    }
    Not working! Can anyone help with this?

  2. #2
    SitePoint Enthusiast myinnet's Avatar
    Join Date
    Jul 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    COuld you please post all the code, include HTML part?

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2002
    Location
    Wales
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Of course, here are the relevant bits:

    <form action="form_process.php?a=edit" method="post" enctype="multipart/form-data" name="testform" id="testform" >

    <input name="add_make" type="text" id="add_make" size="8" maxlength="10" tabindex="4" />
    <select id="make" name="make" onChange="getModelList(this)">
    <option value="">Select</option>
    <?php echo $database -> getMakeSelects();?>
    </select>

    <input name="add_model" type="text" id="add_model" size="8" maxlength="10" />
    <select id="modelID" name="modelID">
    </select>

    I should point out that I've got a bit of ajax (hasn't everybody!!) going on in here but that's working ok, it's just the validation of these bits above that I'm getting lost with. So, to recap I'm looking to validate to make sure that EITHER add_make OR make are filled in and that EITHER add_model OR modelID are filled.

    Hope you can help. Thanks

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, assuming that the first option is "Please choose" or something similiar in the dropdown:
    Code:
    var oModelDropdown = myFm.make;
    
    if(oModelDropdown.selectedIndex == 0)
    {
        alert("Please select something in the model dropdown");
        return false;
    }


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
  •