473,789 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

gingawarrior
3 New Member
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 1536
rnd me
427 Recognized Expert Contributor
if you want to screen against non-blank textareas,


if ( ! elementRef.valu e)

should work.

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

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

Similar topics

9
1966
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 he only enter values into the first four. I need to ALWAYS extract the final or last field that has a value. In the example below this would be PriceField5 which has a value of $162,000. The user stopped at this field and left the rest blank.
5
2641
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 to what I want to do but am unable to get it to work. So if someone could please help me it would greatly be appreciated. Thanks for your help. code: #include <cstdio> #include <cstdlib>
2
5868
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 use template columns as they seem to be a little difficult to create at runtime. Any assistance would be very greatly appreciated. private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if...
4
3518
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 click a button on a pop-up window the javascript for that button click does a 'button.form.submit'. On the Server side there is a Button click event for this button, but for some reason it no longer fires. It worked fine before and everything...
3
2882
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 application or notepad where i want to paste it to be.How can i do it?If anyone knows source code or any website URL's please let me know. Umasakshi
0
1608
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 that when I ran it and looked at memory usage (in Windows task manager) it looked as if there was a very slow leak. To isolate the issue:
1
13966
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" onClick="SetFocusDayPhone(document.getElementsByName('optMedTreatment'))"> <tr> <td><input id="optMedTreatment_0" type="radio" name="optMedTreatment" value="Yes" /><label for="optMedTreatment_0">Yes</label></td> </tr><tr> <td><input id="optMedTreatment_1" type="radio" name="optMedTreatment"
6
4904
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 the html page controls the form fields that are required. It doesn't function like it's supposed to and I can leave all the fields blank and it still submits the form. Also I can't get it to transfer the file in the upload section. The file name...
1
1382
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 Samples.AspNet.Controls.CS {
0
9663
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10404
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9016
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.