By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,282 Members | 1,549 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,282 IT Pros & Developers. It's quick & easy.

Validating dynamically generated fields

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.