#1 8. April 2008 Javascript - String check auf E-Mail-syntax kleines script mit der man einen string auf email-syntax überpfüfen kann. man kann zb. verschiednene hosts angeben und somit wegwerf-emails auschließen. ([g]prototype[/g] wird benötigt) wies geht bitte aus den kommentaren rauslesen (bei den tests)^^ Code: alert("me@domain.tld".isMail()); //true, weil es eine email ist alert("me@domain".isMail()); //false, weil domain falsch ist alert("me@domain.tld".isValidMail('domain')); //true, weil 'domain' keine domain sein kann alert("me@domain.tld".isValidMail('domain', true)); //false, weil mit dem true die tld missachtet wird alert("me@domain.tld".isValidMail({host:'domain', tld:'tld'})); //false, weil host + tld nicht erlaubt alert("me@domain.tld".isValidMail({host:'domain', tld:'net'})); //true, weil domain.net nicht gegeben alert("me@domain.tld".isValidMail({host:'domain', tld:true})); //false, weil tld = true (egal) alert("me@domain".isValidMail()); //false, weil domain falsch ist alert($H("me@foo.bar".matchMail(true)).toQueryString()); //siehe nächste zeile //address=me&domainname=foo&domain=foo.bar&tld=bar Code: /* * short usage * boolean "me@domain.tld".isMail() * * short usage (validator) * boolean "me@domain.tld".isValidMail([mixed forbiddenHosts[, boolean ignoreTLD]]) * * match usage * null|array "me@domain.tld".matchMail() */ /** * tests * * alert("me@domain.tld".isMail()); //true * alert("me@domain".isMail()); //false * alert("me@domain.tld".isValidMail('domain')); //true * alert("me@domain.tld".isValidMail('domain', true)); //false * alert("me@domain.tld".isValidMail({host:'domain', tld:'tld'})); //false * alert("me@domain.tld".isValidMail({host:'domain', tld:'net'})); //true * alert("me@domain.tld".isValidMail({host:'domain', tld:true})); //false * alert("me@domain".isValidMail()); //false * alert($H("me@foo.bar".matchMail(true)).toQueryString()); //see next line * //address=me&domainname=foo&domain=foo.bar&tld=bar */ //class var mailValidator = Class.create({ // address domainname tld mailRegExp: /^([A-Za-z0-9_\.\-]+)@([A-Za-z0-9_\.\-]+)\.([A-Za-z]{2,})$/, forbiddenHosts: [], ignoreTLD: false, //domain == domain.de, domain.net, domain.com [...] /** * @constructor * * @param string mail */ initialize: function(mail) { var exp = mail.match(this.mailRegExp); //check if string match if(exp == false || exp.length < 2) { this.matched = false; return; //no mail => break } this.matched = true; //extend this with some informations about the mail Object.extend(this, { address: exp[1].toLowerCase(), domainname: exp[2].toLowerCase(), domain: exp[2].toLowerCase() + '.' + exp[3].toLowerCase(), tld: exp[3].toLowerCase() }); }, /** * check the mail using forbiddenHosts * * @return boolean */ validate: function() { if(this.matched === false) return false; //no .each on this.forbiddenHosts because we will break on match for(var i = 0, host; host = this.forbiddenHosts[i]; i++) { if(typeof host.tld != 'undefined' && host.tld !== false) { if(host.tld === true) { if(this.domainname == host.host.toLowerCase()) return false; } else { if(host.host.toLowerCase() == this.domainname && host.tld.toLowerCase() == this.tld) { return false; //break } } } else { if(this.ignoreTLD === true && host.host.indexOf('.') == -1) { if(this.domainname == host.host.toLowerCase()) return false; } else if(this.domain == host.host.toLowerCase()) return false; //break } } return true; } }); //string extensions Object.extend(String.prototype, { /** * check if string-syntax looks like a mail * * @return boolean */ isMail: function() { return (this.matchMail() != null); }, /** * match the regexp on given string * * @return mixed [null|array] * array[0] = string, * array[1] = address, * array[2] = domainname, * array[3] = tld */ matchMail: function(toHash) { if(toHash) { var exp = this.match(/^([A-Za-z0-9_\.\-]+)@([A-Za-z0-9_\.\-]+)\.([A-Za-z]{2,})$/); return { address: exp[1].toLowerCase(), domainname: exp[2].toLowerCase(), domain: exp[2].toLowerCase() + '.' + exp[3].toLowerCase(), tld: exp[3].toLowerCase() }; } return this.match(/^([A-Za-z0-9_\.\-]+)@([A-Za-z0-9_\.\-]+)\.([A-Za-z]{2,})$/); }, /** * check mail * * @param mixed hosts [forbidden hosts] * @parram boolean itld [ignore tld] * @return boolean */ isValidMail: function(hosts, itld) { var mail = new mailValidator(this); if(mail.matched == false) return false; var ignoreTLD = ((itld) ? (itld === true) : false); if(hosts) { if(Object.isArray(hosts)) { for(var i = 0, host; host = hosts[i]; i++) { if(Object.isString(host)) mail.forbiddenHosts.push({'host': host, 'tld': ignoreTLD}); else if(typeof host.host != 'undefined' && typeof host.tld != 'undefined') mail.forbiddenHosts.push(host); } } else if(Object.isString(hosts)) mail.forbiddenHosts.push({'host': hosts, 'tld': ignoreTLD}); else if(typeof hosts.host != 'undefined') { if(typeof hosts.tld == 'undefined') host.tld = ignoreTLD; mail.forbiddenHosts.push(hosts); } } return mail.validate(); } }); hier das ganze noch in php: PHP: <? php class mailValidator { //regex pattern public static $mailRegExp = '~^([A-Za-z0-9_\.\-]+)@([A-Za-z0-9_\.\-]+)\.([A-Za-z]{2,})$~' ; public $domain , $domainname , $address , $tld , $matched = true ; //mail pieces public $ignoreTLD = false , $forbiddenHosts = array(); //stuff... /** * constructor * * @param string $mail */ public function __construct ( $mail ) { $results = array(); $exp = preg_match ( self :: $mailRegExp , $mail , $results ); if( $exp == 0 ) $this -> matched = false ; else { $this -> address = strToLower ( $results [ 1 ]); $this -> domainname = strToLower ( $results [ 2 ]); $this -> tld = strToLower ( $results [ 3 ]); $this -> domain = $this -> domainname . '.' . $this -> tld ; } } public function validate () { if( $this -> matched === false ) return false ; foreach( $this -> forbiddenHosts as $host ) { if(isset( $host [ 'tld' ]) && $host [ 'tld' ] !== false ) { if( $host [ 'tld' ] === true ) { if( $this -> domainname == strToLower ( $host [ 'host' ])) return false ; //break } else { if( strToLower ( $host [ 'host' ]) == $this -> domainname && strToLower ( $host [ 'tld' ]) == $this -> tld ) { return false ; //break } } } else { if( $this -> ignoreTLD === true && strpos ( $host [ 'host' ], '.' ) === false ) { if( $this -> domainname == strToLower ( $host [ 'host' ])) return false ; //break; } elseif( $this -> domain == strToLower ( $host [ 'host' ])) return false ; //break } } return true ; } } function strIsMail ( $mail ) { return ( strMatchMail ( $mail ) !== false ); } function strMatchMail ( $mail , $toHash = false ) { $results = array(); $result = preg_match ( mailValidator :: $mailRegExp , $mail , $results ); if( $result == 0 ) return false ; if( $toHash == true ) { $result = new stdClass ; $result -> address = strToLower ( $results [ 1 ]); $result -> domainname = strToLower ( $results [ 2 ]); $result -> tld = strToLower ( $results [ 3 ]); $result -> domain = $result -> domainname . '.' . $result -> tld ; return $result ; } return $results ; } function strIsValidMail ( $mail , $hosts = null , $itld = false ) { $mail = new mailValidator ( $mail ); if( $mail -> matched == false ) return false ; $ignoreTLD = ( $itld === true ); if(!empty( $hosts )) { if( is_array ( $hosts [ 0 ])) { foreach( $hosts as $host ) { if( is_string ( $host )) array_push ( $mail -> forbiddenHosts , array( 'host' => $host , 'tld' => $ignoreTLD )); elseif(isset( $host [ 'host' ]) && isset( $host [ 'tld' ])) array_push ( $mail -> forbiddenHosts , $host ); } } elseif( is_string ( $hosts )) array_push ( $mail -> forbiddenHosts , array( 'host' => $hosts , 'tld' => $ignoreTLD )); elseif(isset( $hosts [ 'host' ])) { if(empty( $hosts [ 'tld' ])) $hosts [ 'tld' ] = $ignoreTLD ; array_push ( $mail -> forbiddenHosts , $hosts ); } } return $mail -> validate (); } ?> funktioniert genauso wie in js. die funktionen sind: Code: boolean strIsMail(string $mail) mixed strMatchMail(string $mail[, boolean $toHash]) boolean strIsValidMail(string $mail[, array $forbiddenHosts[, boolean $ingoreTLD]]) + Multi-Zitat Zitieren