daffy_dowden
09-23-2005, 11:36 AM
Hi All,
Currently learning javascript and I'm probably being a tad ambitious in my current task but I find this is usually the best way to learn. :)
However, I'm a tad stumped. I'm trying to a create script that does form validation. I've borrowed some code from URL=http://www.xs4all.nl/~sbpoley/webmatters/formval.html]this site[/URL] because it gives the desired effect that I'm after. That is once the user types in the text it validates and displays a little caption underneath the title.
I'll paste some of my adapted code.
from code formValid.js
function validateNum (vfld, // element to be validated
ifld, // id of element to receive info/error msg
reqd) // true if required
{
var stat = commonCheck (vfld, ifld, reqd);
if (stat != proceed) return stat;
var tfld = trim(vfld.value);
var ageRE = /^[0-9]{1,3}$/
if (!ageRE.test(tfld)) {
document.getElementById( \' + ifld + \' + ).style.visibility = "visible";)
msg (ifld, "error", "not a valid number");
setfocus(vfld);
return false;
}
return true;
};
in the asp file:
in the head..
<SCRIPT TYPE="text/javascript" src="formValid.js" ></SCRIPT>
<style>
.required {
color: 'red';
font-size: 10px;
display: none;
}
.errHeader {
padding-left: 5px;
padding-bottom: 5px;
font-size: 12px;
}
</style>
in the body...
<SCRIPT TYPE="text/javascript">
function checkform() {
var elem;
var errs=0;
if (!validateNum (document.forms.formerGrad.numReports, 'repNumErr', false)) errs += 1;
//alerts? replace with html instead
if (errs>1) alert('There are fields which need correction before submitting');
if (errs==1) alert('There is a field which needs correction before submitting');
return (errs==0);
};
</SCRIPT>
and finally in the table..
<form action="../scripts/formerGradSubmit.asp" name="formerGrad" method="post" onsubmit="return checkform();">
<tr><td>Number of reports (if applicable)
<div id="repNumErr" class="required">This is Not a Valid Number</div></td>
<td><input type="text" size="5" name="numReports" onchange="validateAge(this, 'repNumErr', false);"/></td>
Any idea why it isn't validating the input fields correctly? I think its to do with the visibility settings but I'm not entirely sure how I should be changing them.
If you need anymore details or info I'd be happy to oblige.
Thanks in advance :thumbsup: ,
Richard
Currently learning javascript and I'm probably being a tad ambitious in my current task but I find this is usually the best way to learn. :)
However, I'm a tad stumped. I'm trying to a create script that does form validation. I've borrowed some code from URL=http://www.xs4all.nl/~sbpoley/webmatters/formval.html]this site[/URL] because it gives the desired effect that I'm after. That is once the user types in the text it validates and displays a little caption underneath the title.
I'll paste some of my adapted code.
from code formValid.js
function validateNum (vfld, // element to be validated
ifld, // id of element to receive info/error msg
reqd) // true if required
{
var stat = commonCheck (vfld, ifld, reqd);
if (stat != proceed) return stat;
var tfld = trim(vfld.value);
var ageRE = /^[0-9]{1,3}$/
if (!ageRE.test(tfld)) {
document.getElementById( \' + ifld + \' + ).style.visibility = "visible";)
msg (ifld, "error", "not a valid number");
setfocus(vfld);
return false;
}
return true;
};
in the asp file:
in the head..
<SCRIPT TYPE="text/javascript" src="formValid.js" ></SCRIPT>
<style>
.required {
color: 'red';
font-size: 10px;
display: none;
}
.errHeader {
padding-left: 5px;
padding-bottom: 5px;
font-size: 12px;
}
</style>
in the body...
<SCRIPT TYPE="text/javascript">
function checkform() {
var elem;
var errs=0;
if (!validateNum (document.forms.formerGrad.numReports, 'repNumErr', false)) errs += 1;
//alerts? replace with html instead
if (errs>1) alert('There are fields which need correction before submitting');
if (errs==1) alert('There is a field which needs correction before submitting');
return (errs==0);
};
</SCRIPT>
and finally in the table..
<form action="../scripts/formerGradSubmit.asp" name="formerGrad" method="post" onsubmit="return checkform();">
<tr><td>Number of reports (if applicable)
<div id="repNumErr" class="required">This is Not a Valid Number</div></td>
<td><input type="text" size="5" name="numReports" onchange="validateAge(this, 'repNumErr', false);"/></td>
Any idea why it isn't validating the input fields correctly? I think its to do with the visibility settings but I'm not entirely sure how I should be changing them.
If you need anymore details or info I'd be happy to oblige.
Thanks in advance :thumbsup: ,
Richard