Javascript and PHP classes to validate e-mail un'indirizzo

Taking up the post Validate email in Javascript and PHP , here's a nice class in JavaScript can verify and check e-mail addresses. Its use is very simple, although it is a client-side control, then easily pass protection, you can just disable Javascript (propose petition against this possibility ;) now all browsers allow JavaScript to evade, and then all the controls associated with - in the short run is not anything on the Internet, see Ajax : Or ).

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/ *
** @ Name: ckmail.js
** @ Description: Check email syntax
** @ Author: = undo =
Web-** @ author: http://www.undolog.com
** @ Author-email: @ g.fazioli undolog.com - g (dot) Fazioli (at) undolog (dot) com
** @ Date: 21/09/2006 21:24
** @ Version: 1.0
* /

{ var oCKMail = {
, __release: "1.0",
, __filter: / ^ ([\ w-] + (?: \. [\ w-] +) *) @ ((?: [\ w-] + \.) * \ w [\ w-] {0, 66}) \. ([az] {2.6} (?: \. [az] {2})?) $ / i,
, __username: "",
, __domain: "",
, __ext: "",

/ / TO DO
( e ) { getUsername: function (e) {
this .__init ( e ) ) return ( this .__username ) ; if (this. __init (e)) return (this. __username);
false ) ; return (false);
},

/ / TO DO
( e ) { getDomain: function (e) {
this .__init ( e ) ) return ( this .__domain ) ; if (this. __init (e)) return (this. __domain);
false ) ; return (false);
},

/ / TO DO
( e ) { GetExtension: function (e) {
this .__init ( e ) ) return ( this .__ext ) ; if (this. __init (e)) return (this. __ext);
false ) ; return (false);
},

/ / TO DO
( e ) { __init: function (e) {
! this .__filter. test ( e ) ) return ( false ) ; if (! this. __filter. test (e)) return (false);
e. split ( "@" ) ; var t = e. split ("@");
t [ 0 ] ; this. __username = t [0];
t [ 1 ] ; this. __domain = t [1];
.__domain. split ( "." ) ; t = this. __domain. split (".");
t [ 1 ] ; this. __ext = t [1];
true ) ; return (true);
},

/ / TO DO
/ / Synopsis
/ / Check (and, extlist, domainlist);
/ / Runs a series of standard controls of an e-mail.
/ / Extlist - You can pass an optional second parameter that corresponds to an array of extensions that
/ / Are permitted or excluded (include / exclude extension) according to the first value in the array, all
/ / Other error will be considered.
/ / Eg. [True, "en", "com"] pass (includes list)
/ / Eg. [False, "en", "com"] NOT pass (exclude list)
/ / If -1 is not taken into account
/ / Domainlist - You can pass an optional third parameter that corresponds to an array of domains that
/ / Are admitted or excluded (include / exclude domain) according to the first value in the array, all the
/ / Other error will be considered.
/ / Eg. [True, "alice.it", "mac.com"] pass (includes list)
/ / Eg. [False, "alice.it", "mac.com"] NOT pass (exclude list)
/ / If -1 is not taken into account
/ /
( e ) { check: function (e) {
this .__init ( e ) ) { if (this. __init (e)) {
/ / Check array domainExt check
arguments. length > 1 ) { if (arguments. length> 1) {
typeof ( arguments [ 1 ] ) == "object" ) { if (typeof (arguments [1]) == "object") {
arguments [ 1 ] [ 0 ] ) { // include list if (arguments [1] [0]) {/ / include list
var i = 0 ; i < arguments [ 1 ] . length ; i ++ ) { for (var i = 0; i <arguments [1]. length; i + +) {
this .__ext == arguments [ 1 ] [ i ] . toLowerCase ( ) ) return ( true ) ; if (this. __ext == arguments [1] [i]. toLowerCase ()) return (true);
}
{ // exclude list Else {} / / exclude list
var i = 0 ; i < arguments [ 1 ] . length ; i ++ ) { for (var i = 0; i <arguments [1]. length; i + +) {
this .__ext == arguments [ 1 ] [ i ] . toLowerCase ( ) ) return ( false ) ; if (this. __ext == arguments [1] [i]. toLowerCase ()) return (false);
}
true ) ; return (true);
}
false ) ; return (false);
}
}
/ / Check array domainName check
arguments. length > 2 ) { if (arguments. length> 2) {
typeof ( arguments [ 2 ] ) == "object" ) { if (typeof (arguments [2]) == "object") {
arguments [ 2 ] [ 0 ] ) { // include list if (arguments [2] [0]) {/ / include list
var i = 1 ; i < arguments [ 2 ] . length ; i ++ ) { for (var i = 1, i <arguments [2]. length; i + +) {
this .__domain == arguments [ 2 ] [ i ] . toLowerCase ( ) ) return ( true ) ; if (this. __domain == arguments [2] [i]. toLowerCase ()) return (true);
}
{ // exclude list Else {} / / exclude list

var i = 1 ; i < arguments [ 2 ] . length ; i ++ ) { for (var i = 1, i <arguments [2]. length; i + +) {
this .__domain == arguments [ 2 ] [ i ] . toLowerCase ( ) ) return ( false ) ; if (this. __domain == arguments [2] [i]. toLowerCase ()) return (false);
}
true ) ; return (true);
}
false ) ; return (false);
}
}
true ) ; // email correct return (true) / / correct email
}
false ) ; // error return (false) / / error
}
};

The object oCKMail provides several methods to carry out a parallel series of checks on the address, such as the extension, domain, etc. ...

To be really sure if you have PHP you can add an additional and effective control before you run the mail() . Here is the PHP class useful for this purpose:

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/ *
** @ Name: cckmail.php
** @ Description: Check email syntax
** @ Author: = undo =
Web-** @ author: http://www.undolog.com
** @ Author-email: @ g.fazioli undolog.com - g (dot) Fazioli (at) undolog (dot) com
** @ Date: 21/09/2006 23:58
** @ Version: 1.0
**
EXAMPLES **
**
** / / Check for an address EXCLUDING those e-mails with domain "e-lementi.com" and "mac.com"
**
OCKMail CCKMail ** $ = new ();
** $ OCKMail-> check ($ email, NULL, array (false, "e-lementi.com", "mac.com"));
**
** / / Check for an address INCLUDING e-mail with the domain "e-lementi.com" and "mac.com"
**
OCKMail CCKMail ** $ = new ();
** $ OCKMail-> check ($ email, NULL, array (true, "e-lementi.com", "mac.com"));
**
** / / Check for an address EXCLUDING those e-mails with a "net", "en", "org"
**
OCKMail CCKMail ** $ = new ();
** $ OCKMail-> check ($ email, array (false, "net", "en", "org"));
**
HISTORY ** / CHANGE LOG
**
* /

! function_exists ( 'checkdnsrr' ) ) { if ( function_exists ('checkdnsrr')) ​​{
( $host , $type = '' ) { function checkdnsrr ($ host, $ type ='') {
! empty ( $host ) ) { if ( empty ($ host)) {
$type == '' ) $type = "MX" ; if ($ type =='') $ type = "MX";
( "nslookup -type= $type $host " , $output ) ; @ exec ("nslookup-type = $ type $ host", $ output);
list ( $k , $line ) = each ( $output ) ) { while ( list ($ k, $ line) = Each ($ output)) {
eregi ( "^ $host " , $line ) ) { if ( eregi ("^ $ host", $ line)) {
; return true;
}
}
; return false;
}
}
}
/ /
class {CCKMail
= "1.1" ; __release $ var = "1.1";
= false ; var $ status = false;
; var $ username;
; var $ domain;
; var $ ext;
/ /
CCKMail function () {}
/ /
$e ) { function _test ($ s) {
status = false ; $ This -> status = false;
'/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/' ; $ P = '/ ^. + \ @ (\ [?) [A-zA-Z0-9 \ - \.] + \. ([A-zA-Z] {2,4} | [0-9] {1.3}) (\]?) $ / ';
preg_match ( $p , $e ) ) ) { if (( preg_match ($ p, $ e))) {
explode ( "@" , $e ) ; $ T = explode ("@", $ s);
username = strtolower ( $t [ 0 ] ) ; $ This -> username = strtolower ($ t [0]);
domain = strtolower ( $t [ 1 ] ) ; $ This -> domain = strtolower ($ t [1]);
explode ( "." , $this -> domain ) ; $ T = explode (".", $ this -> domain);
ext = strtolower ( $t [ 1 ] ) ; $ This -> ext = strtolower ($ t [1]);
/ /
checkdnsrr ( $this -> domain . '.' , 'MX' ) ) $this -> status = true ; if ( checkdnsrr ($ this -> domain. '.', 'MX')) $ this -> status = true;
checkdnsrr ( $this -> domain . '.' , 'A' ) ) $this -> status = true ; if ( checkdnsrr ($ this -> domain. '.', 'A')) $ this -> status = true;
checkdnsrr ( $this -> domain . '.' , 'CNAME' ) ) $this -> status = true ; if ( checkdnsrr ($ this -> domain. '.', 'CNAME')) $ this -> status = true;
}
}
/ /
$m , $e = NULL , $d = NULL ) { function check ($ m, $ e = NULL, $ d = NULL) {
_test ( $m ) ; $ This -> _test ($ m);
$this -> status ) { if ($ this -> status) {
/ / Check array domainExt check
! is_null ( $e ) ) { if ( is_null ($ e)) {
$e [ 0 ] ) { // include list if ($ e [0]) {/ / include list
$i = 1 ; $i < sizeof ( $e ) ; $i ++ ) { for ($ i = 1; $ i < sizeof ($ s); $ i + +) {
$this -> ext == strtolower ( $e [ $i ] ) ) return ( true ) ; if ($ this -> ext == strtolower ($ e [$ i])) return (true);
}
{ // exclude list Else {} / / exclude list
$i = 1 ; $i < sizeof ( $e ) ; $i ++ ) { for ($ i = 1; $ i < sizeof ($ s); $ i + +) {
$this -> ext == strtolower ( $e [ $i ] ) ) return ( false ) ; if ($ this -> ext == strtolower ($ e [$ i])) return (false);
}
true ) ; return (true);
}
false ) ; return (false);
}
/ / Check array domainName check
! is_null ( $d ) ) { if ( is_null ($ d)) {
$d [ 0 ] ) { // include list if ($ d [0]) {/ / include list
$i = 1 ; $i < sizeof ( $d ) ; $i ++ ) { for ($ i = 1; $ i < sizeof ($ d); $ i + +) {
$this -> domain == strtolower ( $d [ $i ] ) ) return ( true ) ; if ($ this -> domain == strtolower ($ d [$ i])) return (true);
}
{ // exclude list Else {} / / exclude list
$i = 1 ; $i < sizeof ( $d ) ; $i ++ ) { for ($ i = 1; $ i < sizeof ($ d); $ i + +) {
$this -> domain == strtolower ( $d [ $i ] ) ) return ( false ) ; if ($ this -> domain == strtolower ($ d [$ i])) return (false);
}
true ) ; return (true);
}
false ) ; return (false);
}
true ) ; return (true);
}
false ) ; return (false);
}
}
?>

What ineteressante is the ability of this class to connect to MX servers (see checkdnsrr() ) to verify the presence of the domain. Thus in addition to perform a syntax check on the address of e-mail runs a real ping the domain past. We can say that this method is 80% sure ...

Comment on: "Classes Javascript and PHP to validate e-mail un'indirizzo"

  1. June 30, 2009 Undolog.com »Wordpress: step by step how to create their own login :

    [...] You may also enter an additional check on the server side / php. See in this regard Classes Javascript and PHP to validate e-mail un'indirizzo PLAIN TEXT [...]

Leave a comment

TAG XHTML PERMITS: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA