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

Form field validation

Status
Not open for further replies.

furtivevole

Technical User
Jun 21, 2001
84
GB
In an HTML form, there is a potential field whose existance depends on a previous user action. Before submitting the form, I have to check that the field both exists and is suitably populated. The client-side code for this is:

//Code A
var strNameX=document.FrontPage_Form1.Name;
if(eval(strNameX+"==null")) {
alert ("Wrong (Code A)");
return false;
}

//Code B
var strNameVal=document.FrontPage_Form1.Name.value;
if (strNameVal.length==0) {
alert ("Wrong (Code B)");
return false;
}
else {
alert ("Successful");
return true;
}


If the field does not exist, then code A executes correctly and the function returns false. However if the field does exist - whether or not populated - Code B executes only if code A is commented out.

I guess I've missed something subtle here - can anyone advise please?
 
Try this modification:

Code:
//Code A
var strNameX=document.FrontPage_Form1.Name;
//if(eval(strNameX+"==null")) {
if([b]!strNameX[/b]) { [b]//this is how we check for whether a field exists[/b]
 alert ("Wrong (Code A)");
 return false;
}
[b]else
{[/b]        
 //Code B
 var strNameVal=document.FrontPage_Form1.Name.value;
 if (strNameVal.length==0) {
  alert ("Wrong (Code B)");
  return false;
 }
 else {
  alert ("Successful");
  return true;
 }
[b]}//end else[/b]

'hope that helps.

--Dave
 
Yes, that worked a treat. I'd not used the else, since when the Code A condition is fulfilled, the function presumably cut out anyway. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top