Welcome, guest | Sign In | My Account | Store | Cart

a small script that can be used for Validating Emails on

  • Login pages
  • forums

changed to preg_match from eregi function. now scripted validates everything typed in input boxes

Old Code =

       foreach($Email as $Emails)
   { // checks Emails - .net , .com , .au  & etc
   if(eregi("[a-zA-Z0-9]@+[a-z].{1,}com$",trim($Emails))|| eregi(            
       "[a-zA-Z0-9]@+[a-z].{1,}net$",trim($Emails)))

New Code =

       foreach($Email as $Emails)
   { // checks Emails - .net , .com , .au  & etc
   if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z]
   [0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $Emails))

i changed from eregi function to preg_match function because eregi was taking out of PHP version 5.3.0 and 5.3.5.

PHP, 52 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<html>
	<head>
		<title>
			Validating Emails
		</title>
		
	</head>
	
	<body>
		<form method="post">
		
		    <!-- Email Inputs - type email to see if they are Validated -->
			Email 1 :<input type="text" name="e1a" value="Enter Email here"/><br /><br />
			Email 2 :<input type="text" name="e2a" /><br /><br />

	        <!-- Submit button -->
			<input type="submit" name="submit" value="Validate"/><br /><br />
			
		</form>
		
		<?php
		function IsValidEmail()
		{
		if(isset($_POST['submit']))
			{
		     // Email imput
			$Email[0] = @$_POST['e1a'];
			$Email[1] = @$_POST['e2a'];
				
			// Checks Eamils to see if the are REAL Emails 
				
			foreach($Email as $Emails)
				{ // checks Emails - .net , .com , .au  & etc
				if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z]
				[0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $Emails))
				{ // checks Emails to see if Validated successfully
                                echo "$Emails
				: Validated successfully !<br /><br />";
				}
				else
				{ // checks Emails to see if Invalid Emails
				echo " $Emails
				: Invalid Email <br /><br />";
				}	
				}	
	     	}
		} 
	?>
</body>

<p> Shows Valid Email </p>
 <?php IsValidEmail() ?>

1 comment

Stephen Reay 9 years, 5 months ago  # | flag

I wonder why you wouldn't just use filter_var ?

if (filter_var($email, FILTER_VALIDATE_EMAIL) !== false) {
    // valid
}
Created by Jonathan Fenech on Sat, 17 Apr 2010 (GPL3)
PHP recipes (51)
Jonathan Fenech's recipes (6)

Required Modules

  • (none specified)

Other Information and Tasks

  • Licensed under the GPL 3
  • Viewed 8638 times
  • Revision 2 (updated 10 years ago)