JS issue: why can't I evaluate if a form item is null/empty?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gingawarrior
    New Member
    • Feb 2008
    • 3

    JS issue: why can't I evaluate if a form item is null/empty?

    Hi All,
    I've written some code to act as a validator for a set of form fields - it accepts a string of '~' delimited item names and then loops through to evaluate if they are either empty or =='999' (default not selected for a dropdown list).

    So far so good...however, although the value=='999' bit works, those fields that post a null or empty value (radio buttons, textareas) don't seem to get picked up.

    Any ideas on what I'm doing wrong here? My JS skills are not exactly red-hot so I'm struggling to figure out what is going on (or not going on more likely!).
    Full code is below:

    Code:
    function STD_validator(f,reqItems) 
    {
      var problems = 0;
      var problemslist = "";
      aryItems = reqItems.split("~")
      for (var i=0; i < aryItems.length; i++) {
        var elementRef = document.getElementById(aryItems[i]);
        if ( elementRef != null )
        {
            if ( (elementRef.value == '999') || (elementRef.value.length <= 0) )
          //if ((document.getElementById(aryItems[i]).value == '999') || (document.getElementById(aryItems[i]).value == ''))
            {
                problems++
                problemslist = problemslist + " " + (i+1);
                document.getElementById("VAL_" + aryItems[i]).style.visibility = 'visible';
                }
            else
            {
                document.getElementById("VAL_" + aryItems[i]).style.visibility = 'hidden';
            };
        }
      }
      
      // SUMMARY - BUILD ERROR REPORT
      if (problems != 0)
      {
        alert (problemslist);
        return false;
      }
      else
      {
      // Submit form
      return true;
      };
    }
    f is the form that is being submitted.
    reqItems in the case of textareas is "FT1~FT2"

    This function is called in form onSubmit. BTW - I'm using Firefox if that's relevant...

    Thanks in advance for any advice you can give.
  • rnd me
    Recognized Expert Contributor
    • Jun 2007
    • 427

    #2
    if you want to screen against non-blank textareas,


    if ( ! elementRef.valu e)

    should work.

    not sure we understand what you need to accomplish.

    Comment

    • gingawarrior
      New Member
      • Feb 2008
      • 3

      #3
      Yeah, I've tried that and it doesn't seem to do it either - something odd definitely going on.
      What I'm trying to achieve is as follows:
      - on form submit, check all fields for entry
      - if field is empty then pop up alert and also show hidden * next to field
      - if field has a value of 999 do the same
      - otherwise (all fields completed) submit the form
      Don't know what I've done that means it doesn't work - do textarea's work differently?

      Comment

      • gingawarrior
        New Member
        • Feb 2008
        • 3

        #4
        Aha, sussed - had some issues with the way my relayted DIV was rendering meaning the JS fell over because it couldn't find it. Sorted now.

        However, I'm still looking for some help on how to evaluate radio button values...
        I'm currently doing the following in JS:

        Code:
        var elementRef = document.getElementsByName(aryItems[i]);
            //var elementRef = document.getElementById(elementRef1);
            if ( elementRef != null )
            {
                if (!elementRef.value)
                //if ((document.getElementById(aryItems[i]).value == '999') || (document.getElementById(aryItems[i]).value == ''))
                {
                    problems++
                    problemslist = problemslist + " " + (i+1);
                    document.getElementById("VAL_" + aryItems[i]).style.visibility = 'visible';
                    }
                else
                {
                    document.getElementById("VAL_" + aryItems[i]).style.visibility = 'hidden';
                };
            }
        Now...this picks up that the value is empty, but still thinks it's empty when I add a value - is this because 'getelementsbyn ame' just gets a reference to an array? Should I be using 'getelementbyid '? The reason I'm not is because my input radio's don't have id's (I write them as literal's in asp.net).

        How would I best deal with this (simply adding the same ID to a group of radio's doesn't work)?

        Comment

        Working...