PDA

View Full Version : Form Validation


simon315
08-17-2002, 01:16 AM
Hi! I need some help creating a validation for a form of mine. I'm looking to do two validations. The first is to check to see if specific fields have been input(all or not all of them). The second validation is to look at the email address and make sure it is valid (containing the '@' symbol and possibly '.com, .edu, .org, etc'). Is this possible? Thanks in advance! Here is my form code:

<table width="360" CELLPADDING="3" CELLSPACING="0" BORDER="0">
<tr>
<td width="115">
<form METHOD="post" ENCTYPE="text/plain" ACTION="mailto:info@mycompany.com" NAME="inforequest" ID="information">
&nbsp;<b>Name:</b>
</td>
<td ALIGN="right">
<input NAME="fullname" ID="fullnameid" SIZE="30" MAXLENGTH="30">
</td>
</tr>
<tr>
<td>
&nbsp;<b>Company Name:</b>
</td>
<td ALIGN="right">
<input NAME="companyname" ID="company" SIZE="30" MAXLENGTH="30">
</td>
</tr>
<tr>
<td>
&nbsp;<b>Position:</b>
</td>
<td ALIGN="right">
<input NAME="position" ID="title" SIZE="30" MAXLENGTH="30">
</td>
</tr>
<tr>
<td>
&nbsp;<b>Telephone:</b>
</td>
<td ALIGN="right">
<input NAME="telephone" ID="phone" SIZE="30" MAXLENGTH="30">
</td>
</tr>
<tr>
<td>
&nbsp;<b>E.Mail Address:</b>
</td>
<td ALIGN="right">
<input NAME="emailaddress" ID="email" SIZE="30" MAXLENGTH="30">
</td>
</tr>
<tr>
<td COLSPAN="2">
&nbsp;<b>Request/Comments:</b>
</td>
</tr>
<tr>
<td COLSPAN="2" ALIGN="right" VALIGN="center"><TEXTAREA id=comments name=requestcomments rows=6 cols=36></TEXTAREA>
</td>
</tr>
<tr>
<td COLSPAN="2" ALIGN="middle" VALIGN="center">
<input TYPE="image" SRC="../images/button_submit_off.gif"
onMouseover="this.src='../images/button_submit_on.gif'"
onMouseout="this.src='../images/button_submit_off.gif'" VALUE="submit" BORDER="0" WIDTH="46"
HEIGHT="15" id=image1 name=image1><IMG src="../images/clear_pixel.gif" width=15><A
onmouseover=document.clear.src=button_reset_on.src onclick="javascript:document.inforequest.reset();return false"
onmouseout=document.clear.src=button_reset_off.src
href="#"><IMG
height=15 src="../images/button_reset_off.gif" width=46 border=0 name=clear></A></FORM>
</td>
</tr>
</table>

scoutt
08-17-2002, 03:56 AM
<form METHOD="post" ENCTYPE="text/plain" ACTION="mailto:info@mycompany.com" NAME="inforequest" ID="information" onSubmit="return valid_user(this)">

the javascript function is called valid_user


<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
<!--
function valid_user(form) {
var name_tag = form.username.value;

if (!name_tag)
{
alert("error message");
return false;
}
return true;
}
// -->
</SCRIPT>

you can add more if you want. you can add email validation but is not worth it as the user has to open their email program to run that form. better to use perl for this

simon315
08-17-2002, 01:01 PM
What do you mean "you can add email validation but is not worth it as the user has to open their email program to run that form. better to use perl for this" I thought that when the user clicked submit, the form got sent through the browser to an e-mail address?

scoutt
08-17-2002, 03:26 PM
no it doesn't. when you use the mailto: action it opens the users default mail program. if you ant to use a script like you just said then you have the action of the form like so

<form action="somescript.cgi"

notice how different it is from yours

<form action="mailto:info@mycompany.com" >

simon315
08-17-2002, 03:29 PM
I understand the difference, but during my testing I would fill the form and click submit. It wouldn't actually open the e-mail program, but it would send it through the e-mail program. I guess I do see the difference. I think I would rather use a cgi script to compile and send the form. Where would you direct someone to go to get an idea of what they have to do, or even snatch a script? Thanks!

scoutt
08-17-2002, 03:31 PM
k no problem.

you can try www.hotscripts.com in the perl section. they have lots of scripts.

or you can try Matt's script archive and get formmail.pl which is a very popular mailing script

simon315
08-17-2002, 03:34 PM
Thanks you for all your help!

Jon Hanlon
08-18-2002, 06:54 PM
To validate Email client-side, use:

function verifyEmail(str) { // returns true or false
var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
return (!r1.test(str) && r2.test(str));
}

// eg.
// if (!verifyEmail(mailString)) {
// alert("Get Real!")
// return false;
// }