...

View Full Version : Enforcing email validation in javascript



gibsonguy
07-09-2012, 12:28 PM
Hi everyone (first post here)
I'm looking for a way to force the user to validate their email address and when this works give them access to send in user comments /questions etc. I have the two separate pieces of code both working but need a way to join them. Just to clarify, the user comment section should remain non-accessible until the user has run the email validation part.

Appreciate all replies!

Email Validation Code
-----------
*/
function evalform(address)
{ crucial = address.indexOf ("@");
if(crucial == -1)
{ window.alert("The E-mail address you entered is not a valid E-mail address.");
return false }
else
{ message = "You entered "+address+" -- Is this correct?";
return window.confirm(message)};
}


</SCRIPT>
</HEAD>
<BODY>
<FORM onSubmit="evalform(this.email.value)">
E-mail:
<INPUT NAME="email" TYPE="text" ROWS=1 SIZE="20">
<INPUT NAME="Submit" TYPE="Submit">
<INPUT NAME="Reset" TYPE="Reset">



Question/Comment Code
----------------------------

<h1><font color="white">Thanks for visiting our website</font></h1>
<h2><font color="white">Please fill in the attached form to register with us or to simply send any questions or comments</h2>

<SCRIPT LANGUAGE="javascript">
function verify()
{
var OpenWindow=window.open("", "newwin", "height=300,width=300");
OpenWindow.document.write("<HTML>")
OpenWindow.document.write("<TITLE>Thanks for Writing</TITLE>")
OpenWindow.document.write("<BODY BGCOLOR='ffffcc'>")
OpenWindow.document.write("<CENTER>")
OpenWindow.document.write("Thank you <B>" + name + "</B> from <B>" +email+ "</B><P>")
OpenWindow.document.write("Your message <P><I>" + document.gbookForm.maintext.value + "</I><P>")
OpenWindow.document.write("from " + name + " / " +email+ "<P>")
OpenWindow.document.write("will be sent along when you close this window.<p>")
OpenWindow.document.write("<CENTER>")
OpenWindow.document.write("<FORM><INPUT TYPE='button' VALUE='Close Window' onClick='self.close()'></FORM>")
OpenWindow.document.write("</CENTER>")
OpenWindow.document.write("</HTML>")

}
</script>

<SCRIPT LANGUAGE='javascript'>


document.write("<FORM METHOD='post' ACTION='mailto:user@url.com?Subject=Mail from " +name+ " at " +email+ "' ENCTYPE='text/plain' NAME='gbookForm'>")

</SCRIPT>

<b>What would you like to tell me?<BR></b>
<TEXTAREA COLS="40" ROWS="20" NAME="maintext"></TEXTAREA><P>
<INPUT TYPE="submit" VALUE="Send It" onClick="verify()">
</FORM>

Philip M
07-09-2012, 02:40 PM
email adddress validation has been covered very many times in this forum.

Your code is very old fashioned. It is hardly adequte to just test for the existence of an @ sign. The normal email validation regex is


if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@((\w)([\w\-]?)+\.)+([a-z]{2,4})$/i.test(emailaddress.value))) {

But that does not protect aginst simple typos and it is probably best to simply ask the user to enter his address and then to confirm it by entering it again.

To prevent the user from completing the comments section until he has entered a valid email address, simply disable the field until the email validation returns true.

Your form action is mailto: The trouble with using this long-obsolete method (mailto) to send form results is its unpredictability. The method it is highly dependent on the browser in use and the email client in use (some people have only Yahoo, Gmail or Hotmail). In particular, your visitor must have Outlook or Outlook Express or Windows Live Mail as the default client for this to work correctly. Even if your visitor is using Internet Explorer, but the default mail client is different (e.g. Eudora or Thunderbird), your mailto form will not work. With all of the browser troubles, you're likely to lose about half of your users' messages. Most of the email clients that can successfully send a mail will prompt the user with a somewhat threatening security dialog prior to sending - this can scare many users from continuing. Also, what about people with Javascript disabled?

In addition, if you place an unobfuscated email address in your webpage, the bots will quickly find it and inundate you in spam.

Modern browsers no longer accept mailto: as a form action - they simply open the email program (if any) and ignore the form. If you are going to use a form then use a server-side CGI formmail script as the action - there are several good free ones out there.

document.write() is in effect obsolete. document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

<script language=javascript> is long deprecated. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9).


In short - rethink your code according to modern practices. As it is it dates from the last century.

BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.



All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum