How to check a empty value/undefined value from a form?

Discussion in 'JavaScript' started by maximusdm, Apr 22, 2010.

  1. #1
    hello all

    I am trying to check if a user picked out a value from a listbox in a form. I iniatially fill up the list box with all values but when showed in the form the first value is empty. So when the user click the SEARCH button to submit the for I call the Search() function below:

    function Search()
    {
    var Scope = document.all.Scope.value;

    alert(typeof(Component)); //returns string
    alert(Component.value); //returns undefined

    if(Component.value == undefined)
    {
    alert("Component value is required for Search!");
    }
    else
    {
    alert("Proceed with Search.");
    }
    }


    Even though I pick a value in the listbox it still returns undefined. Why?

    Thank you
     
    maximusdm, Apr 22, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    Write here html part of the code too. It will be easier to help you.
     
    s_ruben, Apr 22, 2010 IP
  3. maximusdm

    maximusdm Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    There you go:

    <SELECT id=Scope name=Scope ovalue="">
    <OPTION value="" selected></OPTION>
    <OPTION value=1>Global</OPTION>
    <OPTION value=2>Branch</OPTION>
    <OPTION value=3>Group</OPTION>
    <OPTION value=4>Role</OPTION>
    <OPTION value=5>Machine</OPTION>
    </SELECT> </TD>

    Thank you
     
    maximusdm, Apr 23, 2010 IP
  4. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #4
    I cannot see where do you call the function. Can you explain in details what you want? I don't understand.
     
    s_ruben, Apr 23, 2010 IP
  5. maximusdm

    maximusdm Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    sorry, this might help:

    <input type="button" id="cmdSearch" class="btn" value="Search" onclick="Search();"/>

    <script language="javascript">
    var strRemoteFunctionsPath = "CustomXMLAjax.cfm";

    function Search()
    {
    var Scope = document.all.Scope.value;
    var Action = document.all.Action.value;
    var Component = document.all.Component.value;



    if(window.dialogArguments)
    {
    addItemValues=window.dialogArguments.addItemValues;
    }

    if(Component.value == undefined)
    {
    alert("Component value is required for Search!");
    }
    else
    {
    DWREngine._execute(strRemoteFunctionsPath, null, 'Search', Scope, Action, Component, Search_Callback);
    }

    }


    function Search_Callback(data)
    {
    document.all.SearchResultsCell.innerHTML=data;
    }
     
    maximusdm, Apr 23, 2010 IP
  6. drknght

    drknght Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Try using this in your Search() function:

     
    drknght, Apr 23, 2010 IP
  7. maximusdm

    maximusdm Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    you pointed me to the right directions. The getElementById helps to solve my problem.

    Thank you!
     
    maximusdm, Apr 26, 2010 IP
  8. drknght

    drknght Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You're welcome - glad it helped :)
     
    drknght, Apr 26, 2010 IP