surfandturfsd
06-15-2009, 07:01 PM
I created a form and I have it linked to a Javascript validation file but it does not validate it. It seems that everything is correct but obviously I'm missing something. Can someone check it out, thanks.
Here is the form:
<form name="free_dvd" id="free_dvd" method="post" action="/cgi-sys/formmail.pl">
<input type="hidden" name="recipient" value="" />
<input type="hidden" name="subject" value="Five Free DVDs" />
<input type="hidden" name="env_report" value="REMOTE_HOST,REMOTE_ADDR,HTTP_USER_AGENT" />
<p class="Verdana11pt_Plain_drak">
<label>First Name: <br />
<input type="text" name="first_name" id="first_name" size="40" maxlength="40" title="Type First Name Here" accesskey="f" />
</label>
*<br />
<label>Last Name: <br />
<input type="text" name="last_name" id="last_name" size="40" maxlength="40" title="Type Last Name Here" accesskey="l" />
</label>
*<br />
<label>ADDRESS:<br>
<textarea name="address" id="address" cols="45" rows="5"></textarea>
*<br />
</form>
Now here is the Javascript that I'm using to validate the text fields:
var validations = new Array();
// Define which validations to perform. Each array item
// holds the form field to validate, and the validation
// to be applied. This is the only part you need to
// customize in order to use the script in a new page!
validations[0]=["document.free_dvd.first_name", "notblank"];
validations[1]=["document.free_dvd.last_name", "notblank"];
validations[2]=["document.free_dvd.address", "notblank"];
// Customize above array when used with a new page.
function isEmpty(s)
{
if (s == null || s.length == 0)
return true;
// The test returns true if there is at least one non-
// whitespace, meaning the string is not empty. If the
// test returns true, the string is empty.
return !/\S/.test(s);
}
function looksLikeEmail(field)
{
var s = field.value;
if (isEmpty(s))
{
alert("Email may not be empty");
field.focus();
return false;
}
if (/[^@]+@\w+/.test(s))
return true;
alert("E-mail not in valid form.");
field.focus();
return false;
}
function isInteger(field)
{
var s = field.value;
if (isEmpty(s))
{
alert("Field cannot be empty");
field.focus();
return false;
}
if (!(/^-?\d+$/.test(s)))
{
alert("Field must contain only digits");
field.focus();
return false;
}
return true;
}
function validate()
{
var i;
var checkToMake;
var field;
for (i = 0; i < validations.length; i++)
{
field = eval(validations[i][0]);
checkToMake = validations[i][1];
switch (checkToMake)
{
case 'notblank': if (isEmpty(field.value))
{
alert("Field may not be empty");
field.focus()
//field.style.backgroundColor = "#AA0000";
return false;
}
break;
case 'validemail': if (!looksLikeEmail(field))
return false;
break;
case 'isnumber': if (!isInteger(field))
return false;
}
}
return true;
}
Here is the form:
<form name="free_dvd" id="free_dvd" method="post" action="/cgi-sys/formmail.pl">
<input type="hidden" name="recipient" value="" />
<input type="hidden" name="subject" value="Five Free DVDs" />
<input type="hidden" name="env_report" value="REMOTE_HOST,REMOTE_ADDR,HTTP_USER_AGENT" />
<p class="Verdana11pt_Plain_drak">
<label>First Name: <br />
<input type="text" name="first_name" id="first_name" size="40" maxlength="40" title="Type First Name Here" accesskey="f" />
</label>
*<br />
<label>Last Name: <br />
<input type="text" name="last_name" id="last_name" size="40" maxlength="40" title="Type Last Name Here" accesskey="l" />
</label>
*<br />
<label>ADDRESS:<br>
<textarea name="address" id="address" cols="45" rows="5"></textarea>
*<br />
</form>
Now here is the Javascript that I'm using to validate the text fields:
var validations = new Array();
// Define which validations to perform. Each array item
// holds the form field to validate, and the validation
// to be applied. This is the only part you need to
// customize in order to use the script in a new page!
validations[0]=["document.free_dvd.first_name", "notblank"];
validations[1]=["document.free_dvd.last_name", "notblank"];
validations[2]=["document.free_dvd.address", "notblank"];
// Customize above array when used with a new page.
function isEmpty(s)
{
if (s == null || s.length == 0)
return true;
// The test returns true if there is at least one non-
// whitespace, meaning the string is not empty. If the
// test returns true, the string is empty.
return !/\S/.test(s);
}
function looksLikeEmail(field)
{
var s = field.value;
if (isEmpty(s))
{
alert("Email may not be empty");
field.focus();
return false;
}
if (/[^@]+@\w+/.test(s))
return true;
alert("E-mail not in valid form.");
field.focus();
return false;
}
function isInteger(field)
{
var s = field.value;
if (isEmpty(s))
{
alert("Field cannot be empty");
field.focus();
return false;
}
if (!(/^-?\d+$/.test(s)))
{
alert("Field must contain only digits");
field.focus();
return false;
}
return true;
}
function validate()
{
var i;
var checkToMake;
var field;
for (i = 0; i < validations.length; i++)
{
field = eval(validations[i][0]);
checkToMake = validations[i][1];
switch (checkToMake)
{
case 'notblank': if (isEmpty(field.value))
{
alert("Field may not be empty");
field.focus()
//field.style.backgroundColor = "#AA0000";
return false;
}
break;
case 'validemail': if (!looksLikeEmail(field))
return false;
break;
case 'isnumber': if (!isInteger(field))
return false;
}
}
return true;
}