0

I want to validate US phone number, US state code and US zip code using PHP for it I wrote following functions but they are not working properly.

/** check phone number validation**/
  function phone_no($str) { 
    return (bool)eregi( "^([-\(\)\+, 0-9])+$", $str );
  }
/** check zip code validation**/
function integer($str) {
    return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
  }
/** check state code validation **/

    function alpha_num_symbol($str) {
        return ( ! preg_match("/^([a-z0-9\+_\.*-\/'& ])+$/i", $str)) ? FALSE : TRUE;
    }

I'm passing the value from input field of form using variable $str. Can someone please help me in correcting my code so that I can properly validate US phone number, US zip code and US state code. If anyone have any idea to make this thing workable please help me. Thanks in advance.

PHPLover
  • 1
  • 51
  • 158
  • 311
  • http://stackoverflow.com/questions/3357675/validating-us-phone-number-with-php-regex - this might help you to validate US phone number – prava Jun 04 '14 at 05:45
  • @Prava-MindfireSolutions you can remove `` so it will be a clickable link – NullPoiиteя Jun 04 '14 at 05:47
  • For US phone number, please see the link: http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation For Zipcode, please see the link: http://stackoverflow.com/questions/19144794/zipcode-validation-for-us – Dinesh Saini Jun 04 '14 at 05:48

1 Answers1

1
// For USA zip code validation
function validateUSAZip($zip_code) {
    return preg_match(“/^([0-9]{5})(-[0-9]{4})?$/i”,$zip_code);
}

For USA state code validation, http://www.qwc.me/2013/12/us-states-list-static-php-array-with.html - this might help you.

prava
  • 3,916
  • 2
  • 24
  • 35