...

View Full Version : Form Validation



spanish_steps
06-07-2003, 09:49 PM
Hola,

Before i submit a form, i would like to check if all the fields are filled and also i would like to check if my e-mail is entered in a proper format.
It checks for the name and the text but it does'nt check for the e-mail.
Could somebody please tell me where i'm going wrong.

Many thanks



<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Contact Page</title>
<SCRIPT LANGUAGE="JavaScript">
function verify() {
var themessage = "You are required to complete the following fields: ";
if (document.form.name.value=="") {
themessage = themessage + " - Your Name";
}
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.form.email.value)){

themessage = themessage + " - Your E-mail";
}
if (document.form.description.value=="") {
themessage = themessage + " - Your Description";
}
//alert if fields are empty and cancel form submit
if (themessage == "You are required to complete the following fields: ") {
document.form.submit();
}
else {
alert(themessage);
return false;
}
}
</script>

</head>

<body bgcolor="#FFFFCC">
<form name=form method="post" action="contactform.html">
Fields with '<font color="#ff0000">*</font>' are mandatory.
<table border="1" width="100%">
<tr>
<td width="50%">Your Name <font color="#FF0000">*</td>
<td width="50%">
<p><input type="text" name="name" size="20"></p>
</td>
</tr>
<tr>
<td width="50%">Your E-mail <font color="#FF0000">*</td>
<td width="50%"><input type="text" name="email" size="20"></td>
</tr>
<tr>
<td width="50%">Your Description <font color="#FF0000">*</td>
<td width="50%">
<p><textarea rows="7" name="description" cols="60"></textarea></p>

</td>
</tr>
</table>

<table border="1" width="100%">
<tr>
<td width="100%"><input type=button value="Submit" onclick="verify();"></td>
</tr>
</table>
</form>
</body>

</html>

cheesebagpipe
06-07-2003, 09:57 PM
Your regex matches a valid email address, so you need to 'flip' the value returned by the .test() method:

if (!/^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/.test(document.form.email.value)){

Might want to provide better feedback for your users on no email vs. 'bad' email addresses. Check the site search here.

cya, cbp

spanish_steps
06-07-2003, 10:45 PM
hi ya cbp,

That has'nt worked either.Though i give a valid e-mail address, it always give me the mesg, enter the e-mail.

Any ideas why??

Thanks

cheesebagpipe
06-07-2003, 11:06 PM
if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.form.email.value)) {

chrismiceli
06-08-2003, 05:31 AM
if it doesn't complain when you give it a bad e-mail then do what cheeseabagpipe says, if not we need to see the code.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum