Java hell-p?
- Started
- Last post
- 3 Responses
- jacklalane
Trying to validate a form and it's really starting to kill me, for some reason I can't get it to work for the "name" field, the "email" field works.
Here's what I'm using (and yes I have tried Google)<script type="text/javascript">
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_email(email,"Not a valid e-mail address!")==false)
{email.focus();return false;}
}
}
function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
{
alert(alerttxt);return false;
}
else
{
return true;
}
}
}
</script>
- jacklalane0
And thanks in advance to any of you wizards, I know this is a fairly boring post....
- vaxorcist0
I'd just use some tried and tested code, faster than debugging.....
- vaxorcist0
try this, worked for me... change in validate_form function
... in the head....
<script type="text/javascript">
//------------------------------...
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_email(email,"Not a valid e-mail address!")==false)
{thisform.email.focus();return false;}
if (validate_required(name,"fill in the name")==false)
{thisform.name.focus();return false;}
}
}
//------------------------------...--
function validate_required(field,alerttxt)
{
with(field)
{
if (value==null||value=="")
{
alert(alerttxt);return false;
}
else
{
return true;
}
}
}</script>
....... and in the HTML:
<form method="post" name="myform" action="#" onsubmit="validate_form(this); return false">
<p>email:
<input type="text" id="email" name="email" /></p>
<p>name:
<input type="text" id="name" name="name" /></p>
<p>
<input type="submit" name="submit">
</form>