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:
Expand|Select|Wrap|Line Numbers
- 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;
- };
- }
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.