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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form - alerts - validation problems 1

Status
Not open for further replies.

greatfalls

Technical User
Jul 1, 2001
21
0
0
CA

I have included a script and a form. The problem with it is that when it is inside the whole form it "highlights" the whole form - I guess the onfocus does this - it only happens in Netscape. It cannot skip any boxes, which the script catches, but then it lets you go to the output without stopping you. Same problem if I enter a none number - it alerts you but lets you continue. Any suggestions - Thanks
<html><head>
<script language=&quot;JavaScript&quot;>
<!-- // Begin
function chkCurrency(fld) {
// insure currency input (no numeric sign allowed)
var str = fld.value.replace(/\$|\,|\s/g,'');
if (!/^[0-9]+\.?[0-9]{0,2}$/.test(str)) {
window.alert(&quot;This must be a valid currency value.&quot;);
fld.focus();
fld.select();
return false;
}
fld.value = fmtCurrency(str);
var fldAry = new Array();
fldAry[0] = fld.form.salHistoryCur;
fldAry[1] = fld.form.salHistoryLessOne;
fldAry[2] = fld.form.salHistoryLessTwo;
fldAry[3] = fld.form.salHistoryLessThree;
fldAry[4] = fld.form.salHistoryLessFour;
// check field priorities
if (fld.name==&quot;aveSalary&quot;) {
for (var i=0; i<fldAry.length; i++) {
fldAry.value = &quot;&quot;;
}
} else {
var cnt = 0, sum = 0;
for (var i=0; i<fldAry.length; i++) {
if (fldAry.value.length == 0) break;
cnt++;
sum += Number(fldAry.value.replace(/\$|\,|\s/g,''));
}
for (i; i<fldAry.length; i++) {
if (fldAry.value.length > 0) break;
}
if (i<fldAry.length) {
window.alert(&quot;Cannot skip a salary history year.&quot;);
fldAry[cnt].focus();
fldAry[cnt].select();
return false;
}
str = (sum/cnt).toString();
fld.form.aveSalary.value = fmtCurrency(str);
}
return true;
}
function fmtCurrency(str) {
// convert numeric string to currency format
cents = Math.floor((str*100+0.5)%100);
str = Math.floor((str*100+0.5)/100).toString();
if (cents < 10) cents = &quot;0&quot; + cents;
// return currency format
return (str + '.' + cents);
}
// End --></script>
</head>
<BODY >
<form name=&quot;form&quot; method=&quot;post&quot; action=&quot;output_e.cfm&quot;>
<table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; border=&quot;0&quot; align=&quot;center&quot; width=&quot;512&quot;>
<tr> <td><br>
<table width=&quot;508&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;>
<tr><td></td> <td bgcolor=&quot;CED6E7&quot;>
<table width=&quot;508&quot;
cellpadding=&quot;5&quot; bgcolor=&quot;#CED6E7&quot; cellspacing=&quot;0&quot; border=&quot;0&quot;>
<tr bgcolor=&quot;#CED6E7&quot;>
<td class=&quot;out&quot; height=&quot;20&quot; valign=&quot;middle&quot; colspan=&quot;6&quot;>
<table width=&quot;498&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; border=&quot;0&quot;> <tr> <td class=&quot;out&quot; colspan=&quot;5&quot;>Salary History: </td>
</tr> <tr> <tr>
<td width=&quot;98&quot; align=&quot;center&quot; nowrap>
<input type=&quot;text&quot; name=&quot;salHistoryCur&quot; tabindex=&quot;20&quot; size=&quot;6&quot; onChange=&quot;return chkCurrency(this);&quot;></td>
<td width=&quot;98&quot; align=&quot;center&quot;>
<input type=&quot;text&quot; name=&quot;salHistoryLessOne&quot; tabindex=&quot;21&quot; size=&quot;6&quot; onChange=&quot;return chkCurrency(this);&quot;> </td>
<td width=&quot;98&quot; align=&quot;center&quot;> <input type=&quot;text&quot; name=&quot;salHistoryLessTwo&quot; tabindex=&quot;22&quot; size=&quot;6&quot; onChange=&quot;return chkCurrency(this);&quot;>
</td><td width=&quot;98&quot; align=&quot;center&quot;>
<input type=&quot;text&quot; name=&quot;salHistoryLessThree&quot; tabindex=&quot;23&quot; size=&quot;6&quot; onChange=&quot;return chkCurrency(this);&quot;> </td>
<td width=&quot;98&quot; align=&quot;center&quot;> <input type=&quot;text&quot; name=&quot;salHistoryLessFour&quot; tabindex=&quot;24&quot; size=&quot;6&quot; onChange=&quot;return chkCurrency(this);&quot;>
</td></tr><tr> <td class=&quot;out&quot; colspan=&quot;5&quot; height=&quot;25&quot;>Average Salary: </td>
</tr> <tr> <td colspan=&quot;3&quot;> <span class=&quot;label&quot;>$</span>
<input type=&quot;text&quot; name=&quot;aveSalary&quot; tabindex=&quot;27&quot; size=&quot;8&quot; onChange=&quot;return chkCurrency(this);&quot;> </td> </tr>
</table> <div align=&quot;right&quot;><br>
<input type=&quot;image&quot; tabindex=&quot;20&quot; label=&quot;calculate&quot; value=&quot;calculate&quot; src=&quot;graphics/calculate_e.gif&quot; width=&quot;90&quot; height=&quot;20&quot; border=&quot;0&quot; alt=&quot;Calculate&quot; name=&quot;Calculate&quot;></div></td></tr></table></td><td ></td>
</tr></table> </td> </tr> </table> </form>
 
you need to put onSubmit = &quot;return validationFunction()&quot; in the form tag and make your imgs onClick=&quot;document.formName.submit()&quot;
-Greg :-Q

flaga.gif
 
Thanks, Greg for your help. I had those but didn't include them.
 
so is it fixed or not? if it isn't please post the page somewhere so we can check it out. -Greg :-Q

flaga.gif
 
I did a post, I don't know what I did with it, I added pretty colors too. Any way this is what I said.

No it is't solved yet. Hope this makes sense.
I have many pages of validation (to much to post)of the below piece of a form.
One is the below function, when I would put values that didn't pass the validation into the below script, the alert works but in Netscape, I would get this highlighted and freezing effect. I found that by taking out the window.alerts this didn't happen. (this didn't happen with other alert functions in the form) I don't know why this happens, if anyone could figure this out I would be very grateful. (so for now I have blocked out the window.alerts in this function) My second problem is to do a double validation, since after the focus and select it lets you continue, with bad data (non numbers) put into the fields.
I am dealing with a array and am not sure how to do an on submit validaton - with an array. - The fields can be blank, if not blank have to be numbers and of the 5 boxes (fields) - they all don't have to be filled in but can't skip from box 1 to box 3 - so how would I move the below onchange to also be a final validation when hit the submit button?
function validateForm(theForm) {

if (!calcCurrentService(theForm, 'final')) return false;
if ( isAverageSalary(theForm) &&
isValidAge(theForm) &&
isSalaryHistory (theForm) &&

isValidStartService(theForm) ) {
var aveSalary = theForm.aveSalary.value;
} else {
return false;
}

<input type=&quot;text&quot; name=&quot;aveSalary&quot; tabindex=&quot;27&quot; size=&quot;8&quot; onChange=&quot;return chkCurrency(this);&quot;>

function chkCurrency(fld)
{
var str = fld.value.replace(/\$|\,|\s/g,'');
if (!/^[0-9]+\.?[0-9]{0,2}$/.test(str)) {
//window.alert(&quot;This must be a valid currency value.&quot;);

fld.focus();
fld.select();
return false;
}
fld.value = fmtCurrency(str);
var fldAry = new Array();
fldAry[0] = fld.form.salHistoryCur;
fldAry[1] = fld.form.salHistoryLessOne;
fldAry[2] = fld.form.salHistoryLessTwo;
fldAry[3] = fld.form.salHistoryLessThree;
fldAry[4] = fld.form.salHistoryLessFour;


var cnt = 0, sum = 0;
for (var i=0; i<fldAry.length; i++) {
if (fldAry.value.length == 0) break;
cnt++;
sum += Number(fldAry.value.replace(/\$|\,|\s/g,''));
}
for (i; i<fldAry.length; i++) {
if (fldAry.value.length > 0) break;
}
if (i<fldAry.length) {
//window.alert(&quot;Cannot skip a salary history year.&quot;);

fldAry[cnt].focus();
fldAry[cnt].select();
return false;
}
str = (sum/cnt).toString();
fld.form.aveSalary.value = fmtCurrency(str);
}
return true;
}
function fmtCurrency(str) {
// convert numeric string to currency format
cents = Math.floor((str*100+0.5)%100);
str = Math.floor((str*100+0.5)/100).toString();
if (cents < 10) cents = &quot;0&quot; + cents;
// return currency format
return (str + '.' + cents);
}


<input type=&quot;image&quot; value=&quot;calculate&quot; src=&quot;graphics/calculate_e.gif&quot; width=&quot;90&quot; height=&quot;20&quot; border=&quot;0&quot; alt=&quot;Calculate&quot; name=&quot;Calculate&quot;>



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top