电话号码格式化

电话号码格式化 

电话号码格式化
PHP代码:
function format_phone($phone)
        {
            $phone = preg_replace("/[^0-9]/", "", $phone);
            if(strlen($phone) == 7)
                return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
            elseif(strlen($phone) == 10)
                return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/","$1-$2-$3",$phone);  //  400-152-8978   样式可以自己更改($1) $2-$3, 
            else
                return $phone;
        }

php示例
 
js代码:

const validateMobile = (val) => {
  val = val.replace(/[^\d]/g, '').substr(0,11)
  if (val.length <= 3) {
    return val
  } else if (val.length <= 7) {
    val = val.replace(/(\d{3})(\d{0,4})/, '$1-$2')
  } else {
    val = val.replace(/(\d{3})(\d{0,4})(\d{0,4})/, '$1-$2-$3')
  }
  return val
}

示例地址

posted @ 2017-08-25 09:45  _Eternity味道  Views(620)  Comments(0Edit  收藏  举报