SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    usa
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Check Email Address

    Hello all
    I was just making a little script that can send email to people. And well I ran into some problems with trying to make javascript check if the email address was correct in the input box. you know haven the "@" sign n all. So I was wondering if its possible to check it in php.

    Javascript Code:
    <SCRIPT LANGUAGE="JavaScript">

    <!-- Begin
    function checkEmail(emailwebform) {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailwebform.email.value)){
    return (true)
    }
    alert("Invalid E-mail Address! Please re-enter.")
    return (false)
    }
    // End -->

    </script>

    Thank you

  2. #2
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Look here: http://www.php.net/manual/en/function.eregi.php
    (User Contributed Notes)

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    usa
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I been fulling with that command for a while. And I still don't understand it. Can someone explain it for me. PLZ

    Thank You

  4. #4
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This one worked for me:
    Code:
    <html>
    	<head>
    		<script type="text/javascript">
    			function validateEmail(frm)
    			{
    				var email = frm.email.value;
    				var re = /^([a-z0-9_]|\-|\.)+@(([a-z0-9_]|\-)+\.)+[a-z]{2,4}$/;
    				if(re.test(email))
    				{
    					alert(email + ' is valid');
    					return true;
    				}
    				else
    				{
    					alert(email + ' is invalid');
    					return false;
    				}
    			}
    		</script>
    	</head>
    	<body>
    <?
    $email = $_POST["email"];
    if($email != "")
    {
    	$re = "^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}$";
    	if(eregi($re, $email))
    	  echo "$email is valid";
    	else
    	  echo "$email is invalid";
    }
    else
    {
    	echo "Email was empty";
    	$email = "some-ones.email@some-sub_domain123.some-domain.com";
    }
    ?>
        <form action="email.php" method="post" onsubmit="return validateEmail(this);">
          <input type="text" name="email" value="<?=$email?>" size="100">
          <input type="submit" name="submit" value="OK">
        </form>
      </body>
    </html>

  5. #5
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe this regexp is more correct?
    Code:
    /^([a-z0-9_]|\-|\.)*([a-z0-9_]|\-)+@(([a-z0-9_]|\-)+\.)+[a-z]{2,4}$/
    Don't know if a dot in front of the @ is allowed
    Like blah.@blah.com

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    usa
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Useful but just want to know how to check for a email address and instead of saying its valid or whatever say. say something in the processing page. Like "The following Email Address or Address You submited are incorrect"

    Thank You

  7. #7
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    usa
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?php
    // Check Sending Email Address
    if (!eregi ("^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}$", $sentfrom) {
    echo "Invalid Email Adress";
    }
    else {
    echo "Valid Email Adress";
    }
    // Check Your Email Address
    if (!eregi ("^([a-z0-9_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,4}$", $sendto) {
    echo "Invalid Email Adress";
    }
    else {
    echo "Valid Email Adress";
    }
    ?>

    I tried this method. I found it on the php.net site. Don't know if ti works. I tried it and it didn't lol

  8. #8
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    usa
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you all for your help.
    Got it working!!!

  9. #9
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by seventhsign
    Thank you all for your help.
    Got it working!!!
    Any chance you could post the FINAL code of what you got to work???

    THANKS!

    -RocketMan

  10. #10
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    usa
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here you go RocketMan and everyone else.

    PHP Code:
    <?php

    // Checks the email address your sending a message to
    if( !eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*"
    ."@([a-z0-9]+([\.-][a-z0-9]+))*$",
    $sendto$regs) )
    {

    // If email address is invalid you get an error
    echo "Error: '$sendto' isn't a valid mail address!\n";

    // and it exits the hole process
    exit("unable to send message");
    }

    // checks for the host
    elseif( gethostbyname($regs[2]) == $regs[2] )
    {

    // if no such host it gives you a error
    echo "Error: Can't find the host '$regs[2]'!
    \n"
    ;
    }
    ?>
    I added a little bit to it like the exit(); function so that it wont finish processing the rest of your script.
    JUST KILLS IT!!!

    Enjoy!
    -seventhsign

  11. #11
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe this former thread on email validation is of interest for you:
    http://www.sitepointforums.com/showt...n&pagenumber=1
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  12. #12
    Fully Sweet Car noddy's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Western Australia
    Posts
    759
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by seventhsign
    I been fulling with that command for a while. And I still don't understand it. Can someone explain it for me. PLZ

    Thank You
    Check this out
    http://www.regular-expressions.info/

  13. #13
    SitePoint Enthusiast Refresh's Avatar
    Join Date
    Jul 2004
    Location
    Lausanne, Switzerland
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't really get why no one proposed PEAR::Validate?! It's really worth checking IMHO, has a nice multiline regex... http://pear.php.net/package/Validate


Bookmarks

Posting Permissions

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