Hi,
I have a form which includes a field 'number'. When 'number' is changed
additional fields ('item_0', etc) are generated on the form using
'onchange'.
I want to validate the form using Javascript before submission.
I've set out the part of the validation script used to loop through the
dynamically generated fields below. This is where I am encountering
problems.
The script works fine so long as all the dynamically generated fields
are blank. If any of the dynamically generated fields contains data the
script treats the whole form as validated even if all the other fields
are blank.
I've tried lots of different changes to get it to works without
success. I'd appreciate any suggestions as to what's going wrong.
With regards
Emmett Power
//////////////////////Script extract////////////////////////////
var fval = parseInt(number );
for(var i = 0; i < fval; i++) {
var e = eval('document. myForm.item_'+i );
if ((e.value == null) || (e.value == "") || isblank(e.value )) {
{errormsg = errormsg + " - The field for item " + (i+1) + " is
blank.\n";}
submitOK="False "
}
}
if (submitOK=="Fal se")
{
alert("I am sorry but you need to provide a little more information
in order to proceed. The following items need to be completed:\n\n" +
errormsg)
return false
}
//////////////////////Script extract end///////////////////////// 4 2881
Update on this problem. If I cange the test from:
'if ((e.value == null) || (e.value == "") || isblank(e.value ))'
to
' if (e.value == "") '
It works. If anybody can explain what is happening I'd be very
grateful.
Regards
Emmett Power Em*********@gma il.com said: //////////////////////Script extract////////////////////////////
var fval = parseInt(number ); for(var i = 0; i < fval; i++) { var e = eval('document. myForm.item_'+i ); if ((e.value == null) || (e.value == "") || isblank(e.value )) { {errormsg = errormsg + " - The field for item " + (i+1) + " is blank.\n";} submitOK="False " } }
if (submitOK=="Fal se") { alert("I am sorry but you need to provide a little more information in order to proceed. The following items need to be completed:\n\n" + errormsg) return false }
//////////////////////Script extract end/////////////////////////
You need to learn to use a consistent style, particularly in terms
of where you put opening and closing curly braces. You seem to
have an extra closing brace that prevents submitOK from being set
to "False" when you expect.
You also need to learn not to use eval() or parseInt() [in these
cases] and to use boolean values instead of strings. Using regular
expressions for validation would actually simplify things, too:
submitOK=true;
for(var i = 0; i < +number; i++) {
if (-1==document.myF orm.elements["item_"+i].value.search(/\S/)) {
// the value does not contain at least one non-blank character
errormsg+= " - The field for item " + (i+1) + " is blank.\n";
submitOK=false;
}
}
if (!submitOK) {
alert("The following items need to be completed:\n\n" + errormsg);
}
return submitOK;
Lee wrote: Em*********@gma il.com said:
//////////////////////Script extract////////////////////////////
var fval = parseInt(number ); for(var i = 0; i < fval; i++) { var e = eval('document. myForm.item_'+i ); if ((e.value == null) || (e.value == "") || isblank(e.value )) { {errormsg = errormsg + " - The field for item " + (i+1) + " is blank.\n";} submitOK="False " } }
if (submitOK=="Fal se") { alert("I am sorry but you need to provide a little more information in order to proceed. The following items need to be completed:\n\n" + errormsg) return false }
//////////////////////Script extract end/////////////////////////
You need to learn to use a consistent style, particularly in terms of where you put opening and closing curly braces. You seem to have an extra closing brace that prevents submitOK from being set to "False" when you expect.
You also need to learn not to use eval() or parseInt() [in these cases] and to use boolean values instead of strings. Using regular expressions for validation would actually simplify things, too:
submitOK=true; for(var i = 0; i < +number; i++) { if (-1==document.myF orm.elements["item_"+i].value.search(/\S/)) { // the value does not contain at least one non-blank character errormsg+= " - The field for item " + (i+1) + " is blank.\n"; submitOK=false; } } if (!submitOK) { alert("The following items need to be completed:\n\n" + errormsg); } return submitOK;
The following seems simpler to me, it separates the validation of the
fields from the looping bit. It may not help in simple cases, but if
more than one test is to be performed it may be useful.
function ...()
{
...
var el, er, errorMessge=[];
for(var i=0; i<+number; i++) {
el = document.myForm .elements['item_'+i];
er = testValid(el);
if ( er ) errorMessage.pu sh(er);
}
if ( er.length ) {
alert("The following items need to be completed:\n\n"
+ errorMessge.joi n('\n'));
return false;
}
}
function testValid( el )
{
if ('input' == el.nodeName.toL owerCase()) {
if ('' == el.value) {
return " - The field for item " + el.name + " is blank."
} else {
return false;
}
}
// Add tests for other element types...
}
--
Rob
Lee, Rob,
I appreciate the suggestions. I'm going to try them out today.
Thanks for your help.
Regards
Emmett This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Peter Kirk |
last post by:
Hi there
I have a form which submits a list of data to a web-application (which then
saves to a database).
The list consists of four input fields per row.
Eg.
<field_1.1><field_2.1><field_3.1><field_4.1>
<field_1.2><field_2.2><field_3.2><field_4.2>
<field_1.3><field_2.3><field_3.3><field_4.3>
....
|
by: Matthew |
last post by:
All,
I have searched google and the newsgroups but can't find anything the
same as what I am experiencing (though I may have missed something).
I have controls (textboxes) within UserControls which are not behaving
as I would expect. Specifically, if there is a command button external
to the usercontrol which is activated by a shortcut key (eg Alt-B),
the command button Click event handler code 'executes' even though the
textbox set...
|
by: Gary Shell |
last post by:
I am experiencing some strange behavior between a UserControl's validating event and a treeview control. Initially, I thought it was related to an issue in the Knowledgebase article 810852 (http://support.microsoft.com/kb/810852), but then I realized that the hotfix mentioned was in .Net v1.1, which I am using.
I took the sample from that article and recreated the situation I see in my application. (Code included below.)
If you run the...
|
by: Daniel Gormley |
last post by:
What I have is a form that is dynamically generated based on which
database table its calling. Therefore, the number of category.name.count
can be different.
So I have this form generated and say with x amount of input boxes.. I
enter data in the boxes and submit.
The data submitted is needing to be placed into an array for insert into
a database.
|
by: adamredwards |
last post by:
I have a page with some form elements that are dynamically generated.
They are inserted into the dom by first cloning a node, changing the
values like name, and then inserted with insertBefore().
When a field gets added, a link that opens a popup window is next to
it. The popup is opened and has the name of the parent element it
should change in it. The user can use the popup to get a list of
results. In the results I would like them...
| |
by: hardieca |
last post by:
Hi,
I'm building a multi-lingual CMS. The user can add as many languages as
he likes. The user will be able to create sections for different
content (General, News Releases, etc...) in the db table tblSection.
For each section, the user will have to input the section name for each
language he is supporting in tblSectionLang.
So, if the user has a section called News, and he is supporting English
and French, two records will have to be...
|
by: verbatim |
last post by:
with the following page, the dynamically generated fields are not recognized when i try to submit the form, or add more elements.
when i hit my submit button, the address bar has only x1 - x5 in the query string, and does not append any of the dynamically generated fields.
any ideas how to fix this?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
<html>
|
by: lianaent |
last post by:
Hi All,
I'm brand new to asp.net 2.0, and have a simple task of just creating a quick and dirty data entry form with SQL Server 2005 on the back end. I added a gridview to my form, and I can populate it dynamically by choosing from a dropdown that I populate dynamically from the database sys.tables table.
When I choose a table from my dropdown, poof, the gridview is beautifully loaded with all the columns and rows, and the edit and delete...
|
by: Srikanth Ram |
last post by:
Hi,
I'm creating a PHP application. In this a dynamic table with the fields in the database is generated in a page. I have placed a checkbox in each row of the table to approve/disapprove according to spec. This checkbox is also created dynamically according to the fields in the database. Now I need to place a checkbox/ button (by checking the checkbox all the checkbox in the page has to be checked). I checked online and I was able to find...
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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,...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |