Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping in javascript: form validation

Status
Not open for further replies.

scroce

MIS
Nov 30, 2000
780
US
I have a form with several textboxes. They are all named txtNumClaims1, txtNumClaims2, txtNumClaims3, .........txtNumClaims12 and they all hold numbers. I wrote a function called isItNumber() to check for the presence of a number.

I think I can use a "for" to loop through each of the txtboxes to check for validation, can't I? The code below is incorrect, but I think it will give you the idea of what I'm trying to accomplish. Can someone help me?

function formValidation() {

var currentForm

currentForm=document.frmChart
for (var boxCount = 1; j < 12; boxCount++) {

if (isItNumber(currentForm.txtNumClaims+boxCount) == false) {
alert(&quot;Please enter a number.);
currentForm.txtNumClaims+boxCount.focus();
return false;
}
}

i.e. i'm trying to concatenate the boxCount variable to the end of the name of the textbox I'm evaluating instead of writing that block of code twelve times.

Any help is much appreciated.
Seek not outside yourself; heaven is within.
 
try
Code:
for (var boxCount = 1; j < 12; boxCount++)
{
    eval('ClaimsBox = currentForm.txtNumClaims'+boxCount);
    if (isItNumber(ClaimsBox) == false) {
      alert(&quot;Please enter a number.);
      ClaimsBox.focus();
    return false;
    }
}
 
It does seem like that should work - although I still can't seem to get it right. In this line, are you sure there should be single quotes? I tried them with and without, but neither seemed to work.

eval('ClaimsBox = currentForm.txtNumClaims'+boxCount); Seek not outside yourself; heaven is within.
 
either single or double quotes should work fine. what error are you getting? what is the isItNumber function expecting? the form element or the form element's value? the above code passes the form element.
 
No error occurs, the form just gets submitted when it shouldn't be. The isItNumber is expecting the form element - here's the code for that:

//this function tests for an empty and then makes sure whatever is entered is a number.

function isItNumber(elm) {
if (elm.value ==&quot;&quot;) {
return false;
}

for (var i=0; i < elm.value.length; i++) {
if (elm.value.charAt(i) < &quot;0&quot; || elm.value.charAt(i) > &quot;9&quot;) {
return false;
}
}
} Seek not outside yourself; heaven is within.
 
ok. try
for (var boxCount = 1; j < 12; boxCount++)
{
eval('ClaimsBox = currentForm.txtNumClaims'+boxCount);
if (isNaN(Number(ClaimsBox.value))) {
alert(&quot;Please enter a number.);
ClaimsBox.focus();
return false;
}
}
 
Nope - it still seems to pass it right thru: two things.

1. there is a slight error in the for statement, it should read

for (var boxCount = 1; boxCount < 12; boxCount++) NOT
for (var boxCount = 1; j < 12; boxCount++)

right?

I had caught that error before - so that's not what's causing it.

Also, why do you need eval at all? can't you just say this:

for (var boxCount = 1; boxCount < 12; boxCount++)
{
claimsBox = currentForm.txtNumClaims + boxCount

if (isNaN(Number(claimsBox.value)) {
alert(&quot;Please enter a number.&quot;);
claimsBox.focus();
return false;
}
}

This is the last code I tried (unsuccessfully):

for (var boxCount = 1; boxCount < 12; boxCount++)
{
eval(claimsBox = currentForm.txtNumClaims + boxCount);
if (isNaN(Number(claimsBox.value)) {
alert(&quot;Please enter a number.&quot;);
claimsBox.focus();
return false;
}
}

I also tried it with the ' and the &quot; where you mentioned. It just let my form process when it should have stopped it.

I'll keep fiddling with it - this code is probably right, it's just something else keeping it from going.

Is the problem with the concatenation? Seek not outside yourself; heaven is within.
 
do you have a link to the page i can see? i am very confused as to why what i wrote isn't working
 
I don't have it hosted outside yet - but I'll post back if i can get you a link in a reasonable amount of time. Seek not outside yourself; heaven is within.
 
it's still not working. I just don't understand why. Could it be due to the fact that the form is being generated by asp code rather than in plain html? Seek not outside yourself; heaven is within.
 
that shouldn't matter. how big is the generated HTML file? would you be able to post it here?
 
OK here's my entire mess of a page. I hope it's not too unclear. Thanks for giving it a shot. I'll hi-lite what I think are the relevant lines[/red]

<html>
<script Language=&quot;JavaScript&quot;>

//this function tests for an empty and then makes sure whatever is entered is a number.
function isItNumber(elm) {
if (elm.value ==&quot;&quot;) {
return false;
}

for (var i=0; i < elm.value.length; i++) {
if (elm.value.charAt(i) < &quot;0&quot; || elm.value.charAt(i) > &quot;9&quot;) {
return false;
}
}
}

//This is the main portion of the function - it incorporates all of the other functions
//outlined above and uses them on the text/combo boxes on the form before it is submitted.

function formValidation() {

var currentForm
var claimsBox
//var boxCount

currentForm=document.frmChart

//This is my unsuccessfull attempt to shorten the code using a FOR:

//for (var boxCount = 1; boxCount < 5; boxCount++)
//{
// claimsBox = eval(currentForm.txtNumClaims+boxCount);
//// eval(ClaimsBox = currentForm.txtNumClaims+boxCount);
// if (isItNumber(claimsBox) == false) {
// alert(&quot;Please enter a number.&quot;);
// claimsBox.focus();
// return false;
// }
//}

for (var boxCount = 1; boxCount < 12; boxCount++)
{

claimsBox = currentForm.elements['txtNumClaims' + boxCount];
if (isNaN(claimsBox.value))) {
alert(&quot;Please enter a number.&quot;);
claimsBox.focus();
return false;
}
}

//claimsBox = currentForm.elements['txtNumClaims' + boxCount];
//=======================================================================================================

</script>
<head>
<title>PoolPact Claims Chart Admin Page</title>
</head>


<body background=&quot;../Images/back.gif&quot;>
<%
'==============================================
'Troubleshoot Session Variables
'==============================================
'If session(&quot;ValidUser&quot;)=&quot;&quot; then
' Response.Write (&quot;Nothing in the variable&quot;)
'else
'Response.Write (session(&quot;ValidUser&quot;))
'end if
'==============================================

'UPDATE tblClaimsChart SET tblClaimsChart.NumClaims = 444, tblClaimsChart.ClaimAmount = 4444
'WHERE (((tblClaimsChart.CoverageID)=1));

'SELECT tblClaimsChart.CoverageID, tblClaimsChart.CoverageDescrip, tblClaimsChart.NumClaims, tblClaimsChart.ClaimAmount
'FROM tblClaimsChart
'WHERE (((tblClaimsChart.CoverageID)=1));


dim cnPoolPact
dim rsChartInfo
dim strQuery
dim intCount
'dim strPropertyQuery
'dim strALQuery
'dim strALDamageQuery
'dim strCrimeQuery
'dim strE&OQuery
'dim strGLQuery
'dim strLawQuery

intCount=1

Set cnPoolPact= server.CreateObject (&quot;ADODB.Connection&quot;)
cnPoolPact.Open Application(&quot;PoolPactDatabaseConnect&quot;)

strQuery=&quot;SELECT tblClaimsChart.CoverageID, tblClaimsChart.CoverageDescrip, tblClaimsChart.NumClaims, tblClaimsChart.ClaimAmount FROM tblClaimsChart ORDER BY tblClaimsChart.CoverageID&quot;' WHERE tblClaimsChart.CoverageID=1&quot;

Set rsChartInfo = Server.CreateObject (&quot;ADODB.Recordset&quot;)
rsChartInfo_Open strQuery, cnPoolPact
%>
<div class=&quot;indent&quot;>
<table border=&quot;0&quot; width=&quot;95%&quot;>
<tr>
<td valign=&quot;top&quot;>
<table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; align=&quot;center&quot; width=&quot;95%&quot;>
<tr valign=&quot;top&quot;>
<td><img src=&quot;../Images/logopool_sm.gif&quot; width=80 height=80 alt=&quot;&quot; border=&quot;0&quot;>
</td>
<td align=&quot;center&quot;><font size=&quot;4&quot; face=&quot;Arial&quot;><b>Nevada Public Agency
Insurance Pool<br>
&<br>
Public Agency Compensation Trust</b></font> </td>
<td><img src=&quot;../Images/logopact_sm.gif&quot; width=70 height=87 alt=&quot;&quot; border=&quot;0&quot;>
</td>
</tr>
</table>
<table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; align=&quot;center&quot; width=&quot;95%&quot;>
<tr>
<td>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table width=&quot;75%&quot; border=&quot;1&quot; align=&quot;center&quot; height=&quot;54&quot; bordercolor=&quot;#663333&quot;>
<tr>
<td height=&quot;2&quot;>
<p align=&quot;center&quot;><b><font size=&quot;4&quot;>Claims Chart Administration<br>
</font></b></p>
<div align=&quot;center&quot;></div>
<div align=&quot;center&quot;><!-- #BeginLibraryItem &quot;/Library/AdminBar.lbi&quot; -->
<table width=&quot;584&quot; border=&quot;0&quot;>
<tr>
<td width=&quot;60&quot; bgcolor=&quot;#CCCCFF&quot;>
<div align=&quot;center&quot;><a href=&quot;../Database/PoolPact.mdb&quot;><font size=&quot;2&quot;><b>Download
DB </b></font></a> </div></td><td width=&quot;45&quot; bgcolor=&quot;#FFCCFF&quot;>
<div align=&quot;center&quot;><a href=&quot;LinksAdmin.asp&quot;><font size=&quot;2&quot;><b>Links
Admin</b></font></a></div></td><td width=&quot;59&quot; bgcolor=&quot;#CCCCFF&quot;>
<div align=&quot;center&quot;><a href=&quot;MembersAdmin.asp&quot;><font size=&quot;2&quot;><b>Members
Admin</b></font></a></div></td><td width=&quot;45&quot; bgcolor=&quot;#FFCCFF&quot;>
<div align=&quot;center&quot;><a href=&quot;EventsAdmin.asp&quot;><font size=&quot;2&quot;><b>Events
Admin</b></font></a></div></td><td width=&quot;50&quot; bgcolor=&quot;#CCCCFF&quot;>
<div align=&quot;center&quot;><a href=&quot;UsersAdmin.asp&quot;><font size=&quot;2&quot;><b>Users
Admin</b></font></a></div></td><td width=&quot;48&quot; bgcolor=&quot;#FFCCFF&quot;>
<div align=&quot;center&quot;><a href=&quot;VideosAdmin.asp&quot;><font size=&quot;2&quot;><b>Videos
Admin</b></font></a></div></td><td width=&quot;78&quot; bgcolor=&quot;#CCCCFF&quot;>
<div align=&quot;center&quot;><a href=&quot;WorkshopsAdmin.asp&quot;><font size=&quot;2&quot;><b>Workshops
Admin</b></font></a></div></td><td width=&quot;80&quot; bgcolor=&quot;#FFCCFF&quot;>
<div align=&quot;center&quot;><font size=&quot;2&quot;><a href=&quot;ClaimsChartAdmin.asp&quot;><b>Claims
Chart Admin</b></a></font></div></td><td width=&quot;38&quot; bgcolor=&quot;#CCCCFF&quot;>
<div align=&quot;center&quot;><a href=&quot;AdminMain.asp&quot;><font size=&quot;2&quot;><b>Admin
Home</b></font></a></div></td></tr>
</table><!-- #EndLibraryItem -->
<%
with Response

'Begin writing the static part of the table:
.Write &quot;<form method='post' action='ClaimsChartProc.asp' name=frmChart onSubmit='return formValidation()'>&quot;
.Write &quot;<table width='600' border='1'>&quot;
.Write &quot;<tr bgcolor='#663333'>&quot;
.Write &quot;<td width='259'><b><font color='#FFFFFF'>Coverage Description</font></b></td>&quot;
.Write &quot;<td width='234'><b><font color='#FFFFFF'># of Claims</font></b></td>&quot;
.Write &quot;<td width='85'><b><font color='#FFFFFF'>Amount</font></b></td>&quot;
.Write &quot;</tr>&quot;

'Begin writing the dynamic part of the table:

Do while rsChartInfo(&quot;CoverageID&quot;)<8
.Write &quot;<tr bgcolor='#FFCCCC'>&quot;
.Write &quot;<td width='259'>&quot; & rsChartInfo(&quot;CoverageDescrip&quot;) & &quot;</td>&quot;
.Write &quot;<td width='234' align='center'>&quot;
.Write &quot;<input type='text' name='txtNumClaims&quot; & intCount & &quot;' size='6' value=&quot; & rsChartInfo(&quot;NumClaims&quot;)& &quot;>&quot;
.Write &quot;</td>&quot;
.Write &quot;<td width='85'>&quot;
.Write &quot;<input type='text' name='txtClaimAmt$&quot; & intCount & &quot;' size='10' value='&quot; & rsChartInfo(&quot;ClaimAmount&quot;) & &quot;'>&quot;
.Write &quot;</td>&quot;
.Write &quot;</tr>&quot;

rsChartInfo.MoveNext

intCount=intCount+1
Loop

'Write the Total Claims Experience Line in Blue

.Write &quot;<tr bgcolor='#CCCCFF'>&quot;
.Write &quot;<td colspan='2'>&quot;
.Write &quot;<div align='center'><b>Total Claims Experience</b></div>&quot;
.Write &quot;</td>&quot;
.Write &quot;<td width='85'>&quot;
.Write &quot;<div align='center'>(auto calc)</div>&quot;
.Write &quot;</td>&quot;
.Write &quot;</tr>&quot;

'Write the second half of the Chart, below the Total Claims Experience Line in Blue
dim intCoverageID

for intCoverageID=8 to 11
Do while rsChartInfo(&quot;CoverageID&quot;)=intCoverageID
.Write &quot;<tr bgcolor='#FFCCCC'>&quot;
.Write &quot;<td width='259'>&quot; & rsChartInfo(&quot;CoverageDescrip&quot;) & &quot;</td>&quot;
.Write &quot;<td width='234'> </td>&quot;
.Write &quot;<td width='85'>&quot;
.Write &quot;<input type='text' name='txtClaimAmt$&quot; & intCoverageID & &quot;' size='10' value='&quot; & rsChartInfo(&quot;ClaimAmount&quot;) & &quot;'>&quot;
.Write &quot;</td>&quot;
.Write &quot;</tr>&quot;

rsChartInfo.MoveNext
Loop
next

'Write the total expected losses line in blue

.Write &quot;<tr bgcolor='#CCCCFF'>&quot;
.Write &quot;<td colspan='2'>&quot;
.Write &quot;<div align='center'><b>Total Expected Pool Losses</b></div>&quot;
.Write &quot;</td>&quot;
.Write &quot;<td width='85'>&quot;
.Write &quot;<div align='center'>(auto calc)</div>&quot;
.Write &quot;</td>&quot;
.Write &quot;</tr>&quot;

'Write Pro Rated Loss Fund Contrib Line

If rsChartInfo(&quot;CoverageID&quot;)=12 then
.Write &quot;<tr bgcolor='#FFCCCC'>&quot;
.Write &quot;<td width='259'>Pro Rated Loss Fund Contributions</td>&quot;
.Write &quot;<td width='234'> </td>&quot;
.Write &quot;<td width='85'>&quot;
.Write &quot;<input type='text' name='txtClaimAmt$&quot; & rsChartInfo(&quot;CoverageID&quot;) & &quot;' size='10' value='&quot; & rsChartInfo(&quot;ClaimAmount&quot;) & &quot;'>&quot;
.Write &quot;</td>&quot;
.Write &quot;</tr>&quot;
end if


'Write the Last line of the table - Annual Loss Fund Ratio:

.Write &quot;<tr bgcolor='#CCCCFF'>&quot;
.Write &quot;<td colspan='2'>&quot;
.Write &quot;<div align='center'><b>Annual Loss Fund Ratio</b></div>&quot;
.Write &quot;</td>&quot;
.Write &quot;<td width='85'>&quot;
.Write &quot;<div align='center'>(auto calc)</div>&quot;
.Write &quot;</td>&quot;
.Write &quot;</tr>&quot;


'Begin writing the static table closing tags

.Write &quot;</table>&quot;
.Write &quot;<p>&quot;

'Write the Buttons:

.Write &quot;<input type='submit' name='Submit' value='Submit'> &quot;
.Write &quot;<input type='reset' name='Reset' value='Clear Entire Chart'>&quot;
.Write &quot;</p>&quot;

'Finish writing the static table closing tags

.Write &quot;</form>&quot;
.Write &quot;<p> </p>&quot;
.Write &quot;</div>&quot;
.Write &quot;<p align='center'> </p>&quot;
.Write &quot;</td>&quot;
.Write &quot;</tr>&quot;
.Write &quot;</table>&quot;

end with
%>
</div>
</td>
</tr>
</table>
<p align=&quot;left&quot;>  </p>
<p align=&quot;center&quot;> </p>
<h3 align=&quot;center&quot;> </h3>
<h3 align=&quot;center&quot;> </h3>
<h3 align=&quot;center&quot;> </h3>
<h3 align=&quot;center&quot;> </h3>
</div>
</body>
</html> Seek not outside yourself; heaven is within.
 
ok. i changed
if (isNaN(claimsBox.value))) to
if (isNaN(Number(claimsBox.value)))
and added the closing } for the function formValidation and it displays the please enter a number alert
 
SUCCESS!!!!!! thank you SO MUCH discord and trollacious. I really appreciate your keen observation and analysis.

Here is the final bit of code that works correctly for this page.


<script Language=&quot;JavaScript&quot;>

//this function tests for an empty and then makes sure whatever is entered is a number.
function isItNumber(elm) {
if (elm.value ==&quot;&quot;) {
return false;
}

for (var i=0; i < elm.value.length; i++) {
if (elm.value.charAt(i) < &quot;0&quot; || elm.value.charAt(i) > &quot;9&quot;) {
return false;
}
}
}

//This is the main portion of the function - it incorporates all of the other functions
//outlined above and uses them on the text/combo boxes on the form before it is submitted.

function formValidation() {

var currentForm
var claimsBox
var amtBox


currentForm=document.frmChart


//This for validates the txtNumClaims boxes on the chart.
for (var claimsBoxCount = 1; claimsBoxCount < 8; claimsBoxCount++)
{

claimsBox = currentForm.elements['txtNumClaims' + claimsBoxCount];

if (isItNumber(claimsBox) == false) {
alert(&quot;Please check the # OF CLAIMS column for an incorrect entry. - Please only enter a number with no $ or . or ,&quot;);
claimsBox.focus();
return false;
}
}


//This for validates the txtClaimAmt$ boxes on the chart.
for (var amtBoxCount = 1; amtBoxCount < 13; amtBoxCount++)
{

amtBox = currentForm.elements['txtClaimAmt$' + amtBoxCount];

if (isItNumber(amtBox) == false) {
alert(&quot;Please check the AMOUNT column for an incorrect entry. - Please only enter a number with no $ or . or ,&quot;);
amtBox.focus();
return false;
}
}
}

</script> Seek not outside yourself; heaven is within.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top