[RESOLVED] form validation question, "field1 is not defined error"
Hi Everyone,
I have this web form that I want to validate only four of the fields (these are just input text fields, one of which is for e-mail).
The code is supposed to bring up an alert box when a fields is empty and after the user clicks ok puts the focus on the offending input field. For the e-mail box, it is supposed to also check for correct e-mail address form.
While testing the first field, the alert text box appears, I click ok but the page sends the incomplete form to my processing page and causes and error. Firebug says that "field1 is not defined". Honestly it looks like it is defined to me.
Can any of you take a look at the code below and see what I am doing wrong? Only my JS code appears below. I've searched online for possible solutions, but the things I've tried produced similar undefined errors. I admit I need more practice in JavaScript (familiar with JS, not proficient), and of course I could use help in a hurry to finish this project...
Thanks in advance for your help!
kc2010
PHP Code:
<script type="text/javascript">
<!--
function validate_required(field,alerttxt)
{
with (field)
{
if (field.value==null || field.value=="")
{
alert(alerttxt);return false;
}
else
{
return true;
}
}
}
function validate_email(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
if (apos<1||dotpos-apos<2)
{alert(alerttxt);return false;}
else {return true;}
}
}
function validate_form(thisform)
{
with (thisform)
{
if (validate_required("field1","Please fill in your first name to continue.")==false)
{field1.focus();return false;}
else if (validate_required("field2","Please fill in your last name to continue.")==false)
{field2.focus();return false;}
else if (validate_required("field12","Please enter your country to continue.")==false)
{field12.focus();return false;}
else if (validate_required("field3","Please enter your e-mail address to continue.")==false)
{field3.focus();return false;}
else if (validate_email("field3","Please enter a valid e-mail address to continue.")==false)
{field3.focus();return false;}
I initially did not have the quotes around the field1, etc., this happened to be the only way I found to get the alert box to appear.
I made those changes and now no alert boxes appear when I tested- it goes straight to my processing page. Firebug doesn't give me errors now.
What else is wrong with this code? Revised code appears below...
TIA!
PHP Code:
<script type="text/javascript">
<!--
function validate_required(field,alerttxt)
{
if (field.value==null || field.value=="")
{
alert(alerttxt);return false;
}
else
{
return true;
}
}
function validate_email(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
if (apos<1||dotpos-apos<2)
{alert(alerttxt);return false;}
else {return true;}
}
}
function validate_form(thisform)
{
with (thisform)
{
if (validate_required(field1,"Please fill in your first name to continue.")==false)
{field1.focus();return false;}
else if (validate_required(field2,"Please fill in your last name to continue.")==false)
{field2.focus();return false;}
else if (validate_required(field12,"Please enter your country to continue.")==false)
{field12.focus();return false;}
else if (validate_required(field3,"Please enter your e-mail address to continue.")==false)
{field3.focus();return false;}
else if (validate_email(field3,"Please enter a valid e-mail address to continue.")==false)
{field3.focus();return false;}
With the addition of thisform. as you suggested, I still get the "field1 is not defined" error message which points to the this line if (validate_required(field1,"Please fill in your first name to continue.")==false), as shown below.
PHP Code:
function validate_form(thisform)
{
with (thisform)
{
if (validate_required(field1,"Please fill in your first name to continue.")==false)
{thisform.field1.focus();return false;}
validate_form doesn't seem to know that field1 is defined from the form. Here is a bit more code from the actual form, just for field1...
Not sure if this will help any of you in helping me, but I cannot see what is going on...(unfortunately, I've been staring at this code for over a couple of days). Hopefully something will jump out in this code for one of you.
Bookmarks