Click to See Complete Forum and Search --> : form validation


tom_edwards32
05-12-2003, 08:11 AM
I am attempting to validate a form. Instead of displaying an alert box stating that an error occurred, I am putting an image of a checkmark next to the item(s) that need to be filled in. I have put in two lines of code, one that works by hardcoding the imagename and one that does not work, but need to have work(commented out).

Thanks in advance for help.

HERE IS CODE:


<SCRIPT LANGUAGE="JavaScript">
function showErrorImage(imagename,imageurl) { document[imagename].src = imageurl; }

function allowSubmit(f) {
var strfielderror;
var el = document.forms[0].elements;
for(var i = 0 ; i < el.length ; ++i) {
if (el[i].name.substring(0,8)=="required") {


// when called this way, ok,

showErrorImage("required_Q010615-091515error","images/checkMark.gif");
//but when I call it this way, not ok
// strfielderror = el[i].name+"error";
//
showErrorImage(strfielderror,"images/checkMark.gif");
}
}
}
</SCRIPT>
<html>
<head>
<title> My Survey </title>
</head>

<body bgcolor="#ffffff" topmargin="0" leftmargin="0">
<form id="form1" name="form1" method="post" onSubmit="return allowSubmit(this)">
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr> <td width='4%' valign='top' >1 </td> <td width='96%' >Choose your favorite color<font color='red'> *</font><img name='required_Q010615-091515error'
src='images/blankImage.gif'></td></tr>
<tr>
<td><input type="submit" name="Submit" value="Submit" ></td></tr> </table> </form> </body> </html>

khalidali63
05-12-2003, 09:13 AM
Obvious error is at this lne
// strfielderror = el[i].name+"error";
You are trying to access an array value without a proper index value,see "i" is not defined in this function,
There could be more errors,but start by fixing this.Make sure i points to a valid array index.

tom_edwards32
05-12-2003, 03:09 PM
Sorry if I am not seeing the point. But my understanding is the following:
1)My understanding is that the i variable is defined in the for( var i = 0 ; i < el.length ; ++i) statement.

2) under the area "when called this way, ok", comment out the following line:
showErrorImage("required_Q010615-091515error","images/checkMark.gif");

3) under the area "when called this way, not ok" uncomment the two lines-
// strfielderror = el[i].name+"error";
// showErrorImage(strfielderror,"images/checkMark.gif");


somehow or another showErrorImage doesn't understand strfielderror that is being passed.





function allowSubmit(f) {
var strfielderror;
var el = document.forms[0].elements;
for(var i = 0 ; i < el.length ; ++i) {
if (el[i].name.substring(0,8)=="required") {


// when called this way, ok,

showErrorImage("required_Q010615-091515error","images/checkMark.gif");
//but when I call it this way, not ok
// strfielderror = el[i].name+"error";
//
showErrorImage(strfielderror,"images/checkMark.gif");

khalidali63
05-13-2003, 01:14 AM
Originally posted by tom_edwards32
1)My understanding is that the i variable is defined in the for( var i = 0 ; i < el.length ; ++i) statement.


You are right....:D ..

Dave's suggestion should work..