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
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Check if dropdown is selected

    Hi,

    I have this code to check if a text box has been filled in

    function validRequired(formField,fieldLabel)
    {
    var result = true;

    if (formField.value == "")
    {
    alert('Please enter a value for the "' + fieldLabel +'" field.');
    formField.focus();
    result = false;
    }

    return result;
    }

    Its called by:

    if (!validRequired(theForm.Name,"Name"))
    return false;

    How can I change it to work for a dropdown?

    I have tried:

    if (formField.SelectIndex.value == "")

    but it didn't work

    any clues?

  • #2
    New Coder
    Join Date
    Jul 2003
    Location
    London
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    To access or check the value in a drop down use the following convention:

    if (fieldName.options[fieldname.selectedIndex].value=="") {

    do something......

    }

    I hope this helps

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks - but how could you put it into a function?

    I have loads of dropdown boxes you see -- I've been trying to modify the function in my previous post but no joy

  • #4
    New Coder
    Join Date
    Jul 2003
    Location
    London
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can write a function just like the one you started of with:

    function validRequired(formField,fieldLabel,fieldType)
    {
    var result = true;

    if (fieldType == "dropDown") {
    if (formField.options[formField.selectedIndex].value == "")
    {
    alert('Please select a value from the the drop down list "' + fieldLabel +'".');
    formField.focus();
    result = false;
    }

    if (fieldType == "text") {
    if (formField.value == "")
    {
    alert('Please enter a value in the "' + fieldLabel +'" field.');
    formField.focus();
    result = false;
    }

    }

    }

    return result;
    }



    When calling the function you will need to pass it the type of field that you want to validate.
    Last edited by vids; 10-14-2003 at 04:50 PM.


  •  

    Posting Permissions

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