Skip to content
Snippets Groups Projects
Commit 3f3e28a6 authored by Peter Rotich's avatar Peter Rotich
Browse files

Merge pull request #673 from greezybacon/issue/733


lint: Add email address validation

Reviewed-By: default avatarPeter Rotich <peter@osticket.com>
parents 54237f8e ba2598fa
No related branches found
No related tags found
No related merge requests found
......@@ -137,7 +137,8 @@ class Validator {
/*** Functions below can be called directly without class instance.
Validator::func(var..); (nolint) ***/
function is_email($email) {
return preg_match('/^([*+!.&#$|\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})$/i',$email);
require_once PEAR_DIR.'Mail/RFC822.php';
return !PEAR::isError(Mail_RFC822::parseAddressList($email));
}
function is_phone($phone) {
/* We're not really validating the phone number but just making sure it doesn't contain illegal chars and of acceptable len */
......
......@@ -15,6 +15,39 @@ class TestValidation extends Test {
$this->assert(Validator::is_username('中国期刊全文数据'));
// Non-letters
$this->assert(!Validator::is_username('j®red'));
// Special chars
$this->assert(Validator::is_username('jar.ed'));
$this->assert(Validator::is_username('jar_ed'));
$this->assert(Validator::is_username('jar-ed'));
// Illegals
$this->assert(!Validator::is_username('j red'));
$this->assert(!Validator::is_username('jared '));
$this->assert(!Validator::is_username(' jared'));
}
function testValidEmail() {
// Common emails
$this->assert(Validator::is_email('jared@domain.tld'));
$this->assert(Validator::is_email('jared12@domain.tld'));
$this->assert(Validator::is_email('jared.12@domain.tld'));
$this->assert(Validator::is_email('jared_12@domain.tld'));
$this->assert(Validator::is_email('jared-12@domain.tld'));
// Very likely illegal
$this->assert(!Validator::is_email('jared r@domain.tld'));
$this->assert(Validator::is_email('jared@host'));
// Odd cases, but legal
$this->assert(Validator::is_email('jared@[127.0.0.1]'));
$this->assert(Validator::is_email('jared@[ipv6:::1]'));
$this->assert(Validator::is_email('*@domain.tld'));
$this->assert(Validator::is_email("'@domain.tld"));
$this->assert(Validator::is_email('"jared r"@domain.tld'));
// RFC 6530
#$this->assert(Validator::is_email('Pelé@example.com'));
#$this->assert(Validator::is_email('δοκιμή@παράδειγμα.δοκιμή'));
#$this->assert(Validator::is_email('甲斐@黒川.日本'));
}
}
return 'TestValidation';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment