Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Enforcing email validation in javascript

    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>
    Last edited by gibsonguy; 07-09-2012 at 12:30 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,081
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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

    Code:
    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.
    Last edited by Philip M; 07-09-2012 at 02:51 PM. Reason: typos

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •