469,572 Members | 1,324 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

for loop for unknown number of fields to validate

I must confirm the user enters a value for each fund they need. I do not know
how many fund entries there will be...it's expandable to handle each users
needs. I must varify each fund they enter is six digits long. I have no
problems with the Is_In_Format function (I found it on the web and am using
it for other things).

My problem is with my validate function which I've cut down (oh it doesn't
work but you can see the direction I am going with it). Is there a better way
to validate this? If not what am I doing wrong?
function validate() {
for(i=1;i<=nrow;i++) {
If (eval("!Is_In_Format( document.forms[0].fund_" + i + ".value, "dddddd")
|| document.forms[0].fund_" + i + ".value.length != 6")){
alert("Fund must be 6 digits");
eval("document.forms[0].fund_" + i + ".focus();")
}
}
Jul 23 '05 #1
6 1951
Abby Lee wrote:
I must confirm the user enters a value for each fund they need. I do not know
how many fund entries there will be...it's expandable to handle each users
needs. I must varify each fund they enter is six digits long. I have no
problems with the Is_In_Format function (I found it on the web and am using
it for other things).

My problem is with my validate function which I've cut down (oh it doesn't
work but you can see the direction I am going with it). Is there a better way
to validate this? If not what am I doing wrong?
function validate() {
for(i=1;i<=nrow;i++) {
If (eval("!Is_In_Format( document.forms[0].fund_" + i + ".value, "dddddd")
|| document.forms[0].fund_" + i + ".value.length != 6")){
alert("Fund must be 6 digits");
eval("document.forms[0].fund_" + i + ".focus();")
}
}

function validate(){
f-document.forms[0];
for(i=1;i<=nrow;i++) {
if(f["fund"+i].value){
if(!/^\d{6}$/.test(f["fund"+i].value){
alert("Fund must be 6 digits");
f["fund_" + i].focus();
return false;
}
}
}
return true;
}

I am assuming empty fields are OK.
Mick
Jul 23 '05 #2
Mick White <mw******@rochester.rr.com> writes:
Abby Lee wrote:
I must confirm the user enters a value for each fund they need. I do not
know
how many fund entries there will be...it's expandable to handle each
users
needs. I must varify each fund they enter is six digits long. I have no
problems with the Is_In_Format function (I found it on the web and am
using
it for other things).

My problem is with my validate function which I've cut down (oh it
doesn't
work but you can see the direction I am going with it). Is there a better
way
to validate this? If not what am I doing wrong?
function validate() {
for(i=1;i<=nrow;i++) {
If (eval("!Is_In_Format( document.forms[0].fund_" + i + ".value,
"dddddd")
|| document.forms[0].fund_" + i + ".value.length != 6")){
alert("Fund must be 6 digits");
eval("document.forms[0].fund_" + i + ".focus();")
}
}

function validate(){
f-document.forms[0];
for(i=1;i<=nrow;i++) {
if(f["fund"+i].value){
if(!/^\d{6}$/.test(f["fund"+i].value){
alert("Fund must be 6 digits");
f["fund_" + i].focus();
return false;
}
}
}
return true;
}

I am assuming empty fields are OK.
Mick


Mick,
Can you explain what you are doing here...I could not get it to work, even
after changing it to check just the one "fund" field they must use.

function validate(){
if(!/^\d{6}$/.test(document.forms[0].fund_1.value){
alert("Fund must be 6 digits");
document.forms[0].fund_1.focus();
}
else document.forms[0].submit();
}
Jul 23 '05 #3
Abby Lee <ab*******@hotmail.com> writes:
Mick White <mw******@rochester.rr.com> writes:
Abby Lee wrote:
I must confirm the user enters a value for each fund they need. I do
not
know
how many fund entries there will be...it's expandable to handle each
users
needs. I must varify each fund they enter is six digits long. I have no

problems with the Is_In_Format function (I found it on the web and am
using
it for other things).

My problem is with my validate function which I've cut down (oh it
doesn't
work but you can see the direction I am going with it). Is there a
better
way
to validate this? If not what am I doing wrong?
function validate() {
for(i=1;i<=nrow;i++) {
If (eval("!Is_In_Format( document.forms[0].fund_" + i + ".value,
"dddddd")
|| document.forms[0].fund_" + i + ".value.length != 6")){
alert("Fund must be 6 digits");
eval("document.forms[0].fund_" + i + ".focus();")
}
}

function validate(){
f-document.forms[0];
for(i=1;i<=nrow;i++) {
if(f["fund"+i].value){
if(!/^\d{6}$/.test(f["fund"+i].value){
alert("Fund must be 6 digits");
f["fund_" + i].focus();
return false;
}
}
}
return true;
}

I am assuming empty fields are OK.
Mick


Mick,
Can you explain what you are doing here...I could not get it to work, even
after changing it to check just the one "fund" field they must use.

function validate(){
if(!/^\d{6}$/.test(document.forms[0].fund_1.value){
alert("Fund must be 6 digits");
document.forms[0].fund_1.focus();
}
else document.forms[0].submit();
}

Nevermind and thank you Mick, I got it working ;)
Jul 23 '05 #4
JRS: In article <ui***************************@news.ks.uiuc.edu> , seen
in news:comp.lang.javascript, Abby Lee <ab*******@hotmail.com> posted at
Mon, 26 Apr 2004 11:08:04 :
I must confirm the user enters a value for each fund they need. I do not know
how many fund entries there will be...it's expandable to handle each users
needs. I must varify each fund they enter is six digits long. I have no
problems with the Is_In_Format function (I found it on the web and am using
it for other things).

My problem is with my validate function which I've cut down (oh it doesn't
work but you can see the direction I am going with it). Is there a better way
to validate this? If not what am I doing wrong?
function validate() {
for(i=1;i<=nrow;i++) {
If (eval("!Is_In_Format( document.forms[0].fund_" + i + ".value, "dddddd")
|| document.forms[0].fund_" + i + ".value.length != 6")){
alert("Fund must be 6 digits");
eval("document.forms[0].fund_" + i + ".focus();")
}
}


Rather than using some dubious function from the Web, why not test
directly with a RegExp /$\d{6}$/ ?

<URL:http://www.merlyn.demon.co.uk/js-valid.htm#VFF> has a general
method, shown in ...#TC, using an Array in an Object to define all the
tests on a form; it should be possible to replace the scanning of the
array with scanning what I suppose to be an array of elements.

You want something like

function validate() { var F, OK
for(i=0;i<nrow;i++) { F = document.forms[0].element[i] // or similar
OK = /$\d{6}$/.test(F.value) // 6 digits only
if (!OK) { alert("Fund " + i + " must be 6 digits") ; F.focus() ;
return false } }
return true }

FAQ 4.40, 4.39, of course.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #5
Dr John Stockton wrote:

<snip>

function validate() { var F, OK
for(i=0;i<nrow;i++) { F = document.forms[0].element[i] // or similar
OK = /$\d{6}$/.test(F.value) // 6 digits only
if (!OK) { alert("Fund " + i + " must be 6 digits") ; F.focus() ;
return false } }
return true }

FAQ 4.40, 4.39, of course.

OP states that empty fields are OK. But if there is an entry, it should
be 6 digits.
See my earlier posting.
Mick
Jul 23 '05 #6
JRS: In article <zV********************@twister.nyroc.rr.com>, seen in
news:comp.lang.javascript, Mick White <mw******@rochester.rr.com> posted
at Mon, 26 Apr 2004 23:27:27 :
Dr John Stockton wrote:

<snip>

function validate() { var F, OK
for(i=0;i<nrow;i++) { F = document.forms[0].element[i] // or similar
OK = /$\d{6}$/.test(F.value) // 6 digits only
if (!OK) { alert("Fund " + i + " must be 6 digits") ; F.focus() ;
return false } }
return true }

FAQ 4.40, 4.39, of course.

OP states that empty fields are OK. But if there is an entry, it should
be 6 digits.


(a) The first $ above should be ^
(b) OK = /^$|^\d{6}$/.test( ) // seems OK; empty or 6-digit

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Bartosz Wegrzyn | last post: by
6 posts views Thread by simina | last post: by
8 posts views Thread by Abby Lee | last post: by
4 posts views Thread by just.an.imbecile | last post: by
5 posts views Thread by eyoung | last post: by
4 posts views Thread by Andy in S. Jersey | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.