...

View Full Version : Resolved Email Validation Help



RomeosKlassicDJ
07-15-2011, 04:10 PM
Let's start by saying that I stink at javascript. I've taken multiple different tutorials, some more than once, and I still can't get it. So, when I need a code, I search for it, and try different ones until I find something that works.

I've been looking for a code for email validation for quite a long time, and I still can't find something that works with my code for validating the other parts of my contact form. I can get it to check for the presence of text, but not for the presence of an @ sign and period. I would like for someone to please help me figure this out.

Right now I have the following code:

Javascript


<!--

function validate_form ( )
{
valid = true;

if ( document.contact.firstnamecontact.value == "" )
{
alert ( "Please fill in the 'First Name' box." );
valid = false;
}

if ( document.contact.lastnamecontact.value == "" )
{
alert ( "Please fill in the 'Last Name' box." );
valid = false;
}

if ( document.contact.emailcontact.value == "" )
{
alert ( "Please fill in a valid email address." );
valid = false;
}

if ( document.contact.subjectcontact.value == "" )
{
alert ("Please fill in the 'Subject' box." );
valid = false;
}

if ( ( document.contact.topic[0].checked == false )
&& ( document.contact.topic[1].checked == false )
&& ( document.contact.topic[2].checked == false ) )
{
alert ( "Please choose your Topic of Inquiry" );
valid = false;
}

if ( document.contact.messagecontact.value == "" )
{
alert ( "Please specify a question/comment." );
valid = false;
}

return valid;
}

//-->

HTML



<form name="contact" method="post" action="contactcompletion.php3" onsubmit="return validate_form ( );">

<table>

<tr>
<td><b>First Name:&nbsp;</td><td><input type="text" name="firstnamecontact"/></td>
</tr>

<tr>
<td><b>Last Name:&nbsp;</b></td><td><input type="text" name="lastnamecontact"/></td>
</tr>

<tr>
<td><b>Email:&nbsp;</b></td><td><input type="text" name="emailcontact"/></td>
</tr>

<tr>
<td><b>Subject:&nbsp;</b></td><td><input type="text" name="subjectcontact"/></td>
</tr>

</table>

<br />
<br />


<h4>Topic of Inquiry (Choose One):</h4>

<table>


<tr>
<td><input type="radio" name="topic" value="questionscontact" /><b>&nbsp;&nbsp;Questions</b></td>

<td><input type="radio" name="topic" value="commentscontact" /><b>&nbsp;&nbsp;Comments</b></td>

<td><input type="radio" name="topic" value="othercontact" /><b>&nbsp;&nbsp;Other</b></td>
</tr>

</table>

<br />
<br />

<h4>Enter Your Questions or Comments:</h4>

<br />

<textarea name="messagecontact" rows="10" cols="70"></textarea>

<br />
<br />

<p><input type="submit" name="send" value="Send" /></p>



Thanks a bunch in advance for helping me!!

Sciliano
07-15-2011, 04:43 PM
RomeosKlassicDJ:

How about scrapping that and using something that already works?

http://www.freewarefiles.com/Plug-n-Play-Contact-Us-Form_program_53638.html

RomeosKlassicDJ
07-15-2011, 09:28 PM
Well, that's cool, but I'd kind of like to have it styled to my webpage--unless I can do this with it (if it generates an easy-to-adjust code).

prasanthmj
07-16-2011, 02:31 AM
Is this the check you want?


if(document.contact.emailcontact.value.indexOf('@')< 0 ||
document.contact.emailcontact.value.indexOf('.') < 0)
{
//error
}

See indexOf reference (http://www.w3schools.com/jsref/jsref_indexof.asp)

You can also use a ready-made Javascript Form Validation Script (http://www.javascript-coder.com/html-form/javascript-form-validation.phtml)

RomeosKlassicDJ
07-16-2011, 09:16 PM
Thank you SO much!!! I had to make a couple of modifications (I'll include the code so anyone else viewing this post in the future can use it) but it worked!!!!

Javascript Modification:



if ( document.contact.emailcontact.value.indexOf('@')< 0 ||
document.contact.emailcontact.value.indexOf('.') < 0)
{
alert ( "Please fill in a valid email address." );
valid = false;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum