/    Sign up×
Community /Pin to ProfileBookmark

simple form validation

hi

i have a form where i need the user to input their name, e-mail address and a text area for comments. i have 3 seperate html pages that work for each field on its own but having a bother getting them to work all together.

name

[CODE]
<!–
function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value==””)
{alert(alerttxt);return false;}
else {return true}
}
}

function validate_form(thisform)
{
with (thisform)
{
if (validate_required(name,”name must be filled out!”)==false)
{name.focus();return false;}
}
}
–>
</script>
[/CODE]

email

[CODE]
<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;}
}
}
//–>
</script>
[/CODE]

comments

[CODE]
<script type=”text/javascript”>
<!–
function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value==””)
{alert(alerttxt);return false;}
else {return true}
}
}

function validate_form(thisform)
{
with (thisform)
{
if (validate_required(comments,”Comments Box must be filled in!”)==false)
{comments.focus();return false;}
}
}
–>
</script>
[/CODE]

Form

[CODE]
<form action=”thankyou.html” onsubmit=”return validate_form(this);” method=”post”>
<p>
Name: <input type=”text” name=”name” size=”25″ /><br />
Email: <input type=”text” name=”email” size=”30″/><br />
Comments:<br /><textarea rows=”8″ cols=”30″ name=”comments”></textarea><br />
<input type=”submit” value=”Submit”/>
</p>
</form>
[/CODE]

any help would be much appreciated i do feel i may be going about it the wrong way however

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@FangDec 12.2007 — function validate_form(f) {
var alerttxt=[
"name must be filled out!",
"Not a valid e-mail address!",
"Comments Box must be filled in!"
];
for(var i=0; i&lt;f.length-1; i++) {
if(f[i].name=='email') {
apos=f[i].value.indexOf("@");
dotpos=f[i].value.lastIndexOf(".");
if (apos&lt;1||dotpos-apos&lt;2) {
alert(alerttxt[i]);
return false;
}
}
else if (f[i].value==null || f[i].value=="") {
alert(alerttxt[i]);
f[i].focus();
return false;
}
}
return true;
}
×

Success!

Help @weeold spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...