473,323 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

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

gingawarrior
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:

Expand|Select|Wrap|Line Numbers
  1. function STD_validator(f,reqItems) 
  2. {
  3.   var problems = 0;
  4.   var problemslist = "";
  5.   aryItems = reqItems.split("~")
  6.   for (var i=0; i < aryItems.length; i++) {
  7.     var elementRef = document.getElementById(aryItems[i]);
  8.     if ( elementRef != null )
  9.     {
  10.         if ( (elementRef.value == '999') || (elementRef.value.length <= 0) )
  11.       //if ((document.getElementById(aryItems[i]).value == '999') || (document.getElementById(aryItems[i]).value == ''))
  12.         {
  13.             problems++
  14.             problemslist = problemslist + " " + (i+1);
  15.             document.getElementById("VAL_" + aryItems[i]).style.visibility = 'visible';
  16.             }
  17.         else
  18.         {
  19.             document.getElementById("VAL_" + aryItems[i]).style.visibility = 'hidden';
  20.         };
  21.     }
  22.   }
  23.  
  24.   // SUMMARY - BUILD ERROR REPORT
  25.   if (problems != 0)
  26.   {
  27.     alert (problemslist);
  28.     return false;
  29.   }
  30.   else
  31.   {
  32.   // Submit form
  33.   return true;
  34.   };
  35. }
  36.  
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.
Feb 22 '08 #1
3 1507
rnd me
427 Expert 256MB
if you want to screen against non-blank textareas,


if ( ! elementRef.value)

should work.

not sure we understand what you need to accomplish.
Feb 22 '08 #2
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?
Feb 22 '08 #3
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:

Expand|Select|Wrap|Line Numbers
  1. var elementRef = document.getElementsByName(aryItems[i]);
  2.     //var elementRef = document.getElementById(elementRef1);
  3.     if ( elementRef != null )
  4.     {
  5.         if (!elementRef.value)
  6.         //if ((document.getElementById(aryItems[i]).value == '999') || (document.getElementById(aryItems[i]).value == ''))
  7.         {
  8.             problems++
  9.             problemslist = problemslist + " " + (i+1);
  10.             document.getElementById("VAL_" + aryItems[i]).style.visibility = 'visible';
  11.             }
  12.         else
  13.         {
  14.             document.getElementById("VAL_" + aryItems[i]).style.visibility = 'hidden';
  15.         };
  16.     }
Now...this picks up that the value is empty, but still thinks it's empty when I add a value - is this because 'getelementsbyname' 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)?
Feb 22 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: jason | last post by:
Access 2000 I need some help interogatting a table and extracting via ASP the final field in a row which has a value. In other words, I have a maximum of 10 fields but, at the user level he may...
5
by: Henry Jordon | last post by:
ok I have my problem entering in an expression in infix notation and it outputs the postfix notation. I now need to evaluate the postfix notation. I have some code written and there are comments as...
2
by: Steve Pierce | last post by:
I am having some issues with a runtime dropdownlist in a datagrid. The issue is that I cannot get ViewState to fill the selected index of a runtime dropdown properly on postback. I do not want to...
4
by: John Boy | last post by:
Hi, Can anyone help. This is really doing my nut in. 3 years ASP exp. and now doing .DOT which is a step in the wrong direction. Basically I am left with the code of a guy who has left. When I...
3
by: Uma sakshi | last post by:
Hi I have one VB.NET application,in that application i have one datagrid control.The datagrid control contains somedata.I want to copy the data in a particular cell and paste it into my C#.NET...
0
by: Maxwell | last post by:
Hello, I recently completed a MC++ (VS2003) DLL that wraps a non MFC C++ DLL and need to use it in a MC++ Console Application (no forms/guis of any kind just output to console). Trouble is...
1
by: dickson.matt | last post by:
OK, I am checking the values of a radiobuttonlist with 2 values (Yes and No) using javascript: <table id="optMedTreatment"...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
1
by: sivakrishna546 | last post by:
using System; using System.Collections; using System.ComponentModel; using System.Security.Permissions; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.