Implement Us Phone and Zip Expression Validator in Php

I want a format:

a.) phone: (xxx)-(xxx)-(xxxx)
b.) zip: (xxxxx)-(xxxx); -(xxxx) is optional

But, I couldn't find them so I created mine anyway.


$us_phone = "^(\()[0-9]{3}(\))(\-)(\()[0-9]{3}(\))(\-)(\()[0-9]{4}(\))$";
$us_zip = "^((\()[0-9]{5}(\)))(-(\()[0-9]{4}(\)))?$";

$code = $us_phone;

function validate($code, $value) {
if(ereg($validator, $value)) {
return true;
else
return false;
}

0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post
NERV Open Source
Building production Spring Boot systems?

Explore NERV — open-source Java libraries for audit trails, persistence, exception handling, and reliable event-driven architecture.