Navigation Menu

Skip to content

Commit

Permalink
add Japan phone validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ichikaway authored and predominant committed Dec 12, 2009
1 parent e0db214 commit 6b2a3f4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
14 changes: 13 additions & 1 deletion libs/jp_validation.php
Expand Up @@ -26,6 +26,18 @@
*/
class JpValidation {

/**
* Checks phone numbers for Japan
*
* @param string $check The value to check.
* @return boolean
* @access public
*/
function phone($check) {
$pattern = '/^(0\d{1,4}[\s-]?\d{1,4}[\s-]?\d{1,4}|\+\d{1,3}[\s-]?\d{1,4}[\s-]?\d{1,4}[\s-]?\d{1,4})$/';
return preg_match($pattern, $check);
}

/**
* Checks zipcodes for Japan
*
Expand All @@ -38,4 +50,4 @@ function postal($check) {
return preg_match($pattern, $check);
}
}
?>
?>
30 changes: 29 additions & 1 deletion tests/cases/libs/jp_validation.test.php
Expand Up @@ -27,6 +27,34 @@
*/
class JpValidationTestCase extends CakeTestCase {

/**
* test the phone method of JpValidation
*
* @return void
* @access public
*/
function testPhone() {

$this->assertTrue(JpValidation::phone('03-1111-2222'));
$this->assertTrue(JpValidation::phone('090-1111-2222'));
$this->assertTrue(JpValidation::phone('0111-11-2222'));
$this->assertTrue(JpValidation::phone('02222-1-1111'));
$this->assertTrue(JpValidation::phone('0311112222'));
$this->assertTrue(JpValidation::phone('09011112222'));
$this->assertTrue(JpValidation::phone('03 1111 2222'));
$this->assertTrue(JpValidation::phone('090 1111 2222'));
$this->assertTrue(JpValidation::phone('+81 90 1111 2222'));
$this->assertTrue(JpValidation::phone('+81-90-1111-2222'));
$this->assertTrue(JpValidation::phone('+819011112222'));

$this->assertFalse(JpValidation::phone('051238-1-111'));
$this->assertFalse(JpValidation::phone('90-1111-2222'));
$this->assertFalse(JpValidation::phone('+8190111122221199'));
$this->assertFalse(JpValidation::phone('+8111-90-1111-2222'));
$this->assertFalse(JpValidation::phone('056'));
}


/**
* test the postal method of JpValidation
*
Expand All @@ -38,4 +66,4 @@ function testPostal() {
$this->assertFalse(JpValidation::postal('0205-504'));
}
}
?>
?>

0 comments on commit 6b2a3f4

Please sign in to comment.