1. 程式人生 > >Js 正則表示式驗證

Js 正則表示式驗證

  1. 手機號碼驗證
    130,131,132,133,134,135,136,137,138,139,147,150,151,152,153,155,156,157,158,159,177,173180,181,182,185,186,187,188,189
function checkMobile(Phone) {
    if (!(/^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[3|7])|(18([0-2]|[5-9])))\d{8}$/.test(Phone))) {
            return false;
    }
}
  1. 驗證Guid
 function TestGuid(testID) {
    var reg = new RegExp(/^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$/);
    if (reg.test(testID)) {
      return true;
    }
    return false;
 }
  1. 驗證URL地址
function ValidateUrl(url) {
    var reg = new RegExp(/^((ht|f)tps?):\/\/([\w\-]+(\.[\w\-]+)*\/)*[\w\-]+(\.[\w\-]+)*\/?(\?([\w\-\.,?^=%&:\/~\+#]*)+)?/);
    if (reg.test(url)) {
        return true;
    }
    return false;
}