473,321 Members | 1,667 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,321 software developers and data experts.

Validating dynamically generated fields

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=="False")
{
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/////////////////////////

Sep 7 '05 #1
4 2854
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

Sep 7 '05 #2
Lee
Em*********@gmail.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=="False")
{
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.myForm.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;

Sep 7 '05 #3
Lee wrote:
Em*********@gmail.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=="False")
{
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.myForm.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.push(er);
}
if ( er.length ) {
alert("The following items need to be completed:\n\n"
+ errorMessge.join('\n'));
return false;
}
}

function testValid( el )
{
if ('input' == el.nodeName.toLowerCase()) {
if ('' == el.value) {
return " - The field for item " + el.name + " is blank."
} else {
return false;
}
}
// Add tests for other element types...
}

--
Rob
Sep 8 '05 #4
Lee, Rob,

I appreciate the suggestions. I'm going to try them out today.

Thanks for your help.

Regards

Emmett

Sep 8 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
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....
0
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...
0
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...
1
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...
1
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(). ...
1
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...
1
verbatim
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...
0
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...
7
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...
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.