1. 程式人生 > >angular寫的身份證校驗規則(省份生日性別)手機號校驗 。直接貼程式碼

angular寫的身份證校驗規則(省份生日性別)手機號校驗 。直接貼程式碼

(function() {
  'use strict';

  angular.module('app.house')
    .controller('HotelOrderCtrl', HotelOrderCtrl);
  HotelOrderCtrl.$inject = ['$scope', '$ionicHistory','$state','$stateParams','houseService','$timeout','$ionicLoading','$filter','$rootScope','mineService','$ionicModal','DataConstantFun'];
  function HotelOrderCtrl($scope, $ionicHistory,$state,$stateParams,houseService,$timeout,$ionicLoading,$filter,$rootScope,mineService,$ionicModal,DataConstantFun) {
    $scope.goBack = function () {
      $ionicHistory.goBack();
    };
    $scope.btnDisabled = false;
    $scope.goods_id = $stateParams.goods_id;
    $scope.startDate = $stateParams.startDate;
    $scope.startDateForShow = $scope.startDate;
    $scope.endDate = $stateParams.endDate;
    $scope.endDateForShow = $scope.endDate;
    $scope.book_unitprice = $stateParams.book_unitprice;
    $scope.goods_name = $stateParams.goods_name;
    $scope.curAirbnbId = $stateParams.airbnb_id;
    $scope.nightNum = Math.ceil(($scope.endDate - $scope.startDate)/86400000);
    $scope.bookdate_lst = [];
    for(var i=0;i<=$scope.nightNum - 1;i++){
      $scope.dataForPush = Number($scope.startDate) + 86400000 * i;
      $scope.dataForBookDateLst = $filter('date')($scope.dataForPush,'yyyy-MM-dd') + " 12:00:00";
      $scope.bookdate_lst.push({"book_date":$scope.dataForBookDateLst});
    }
    $scope.nameCheck = function(){
      if($scope.selectedMailMsg.book_user != null){
        if($scope.selectedMailMsg.book_user > 15){
          $scope.selectedMailMsg.book_user = null;
          $ionicLoading.show({
            template: "請輸入15字以內的姓名"
          });
          $timeout(function(){
            $ionicLoading.hide();
          },2000);
        }
      }
    };
    $scope.telNumCheck = function(){
      var reg = /^1[0-9]{10}$/;
      if($scope.selectedMailMsg.book_user_tel != null){
        if(isNaN($scope.selectedMailMsg.book_user_tel)){
          $scope.selectedMailMsg.book_user_tel = null;
          $ionicLoading.show({
            template: "請輸入正確的手機號"
          });
          $timeout(function(){
            $ionicLoading.hide();
          },2000);
        }else if(!reg.test($scope.selectedMailMsg.book_user_tel)){
          $scope.selectedMailMsg.book_user_tel = null;
          $ionicLoading.show({
            template: "請輸入正確的手機號"
          });
          $timeout(function(){
            $ionicLoading.hide();
          },2000);
        }
      }
    };

    var vcity={ 11:"北京",12:"天津",13:"河北",14:"山西",15:"內蒙古",
      21:"遼寧",22:"吉林",23:"黑龍江",31:"上海",32:"江蘇",
      33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山東",41:"河南",
      42:"湖北",43:"湖南",44:"廣東",45:"廣西",46:"海南",50:"重慶",
      51:"四川",52:"貴州",53:"雲南",54:"西藏",61:"陝西",62:"甘肅",
      63:"青海",64:"寧夏",65:"新疆",71:"臺灣",81:"香港",82:"澳門",91:"國外"
    };
    $scope.personNumCheck = function() {
      var card = $scope.selectedMailMsg.book_user_idcard;
      //是否為空
      if(card == '')
      {
        $ionicLoading.show({
          template: "身份證號不能為空"
        });
        $timeout(function(){
          $ionicLoading.hide();
        },2000);
        $scope.selectedMailMsg.book_user_idcard = '';
      }
      //校驗長度,型別
      if($scope.isCardNo(card) === false)
      {
        $ionicLoading.show({
          template: "您輸入的身份證號碼不正確,請重新輸入"
        });
        $timeout(function(){
          $ionicLoading.hide();
        },2000);
        $scope.selectedMailMsg.book_user_idcard = '';
      }
      //檢查省份
      if($scope.checkProvince(card) === false)
      {
        $ionicLoading.show({
          template: "您輸入的身份證號碼不正確,請重新輸入"
        });
        $timeout(function(){
          $ionicLoading.hide();
        },2000);
        $scope.selectedMailMsg.book_user_idcard = '';
      }
      //校驗生日
      if($scope.checkBirthday(card) === false)
      {
        $ionicLoading.show({
          template: "您輸入的身份證號碼不正確,請重新輸入"
        });
        $timeout(function(){
          $ionicLoading.hide();
        },2000);
        $scope.selectedMailMsg.book_user_idcard = '';
      }
      //檢驗位的檢測
      if($scope.checkParity(card) === false)
      {
        $ionicLoading.show({
          template: "您輸入的身份證號碼不正確,請重新輸入"
        });
        $timeout(function(){
          $ionicLoading.hide();
        },2000);
        $scope.selectedMailMsg.book_user_idcard = '';
      }
    };


//檢查號碼是否符合規範,包括長度,型別
    $scope.isCardNo = function(card)
    {
      //身份證號碼為15位或者18位,15位時全為數字,18位前17位為數字,最後一位是校驗位,可能為數字或字元X
      var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/;
      if(reg.test(card) === false)
      {
        return false;
      }else{

      return true;
      }
    };

//取身份證前兩位,校驗省份
    $scope.checkProvince = function(card)
    {
      var province = card.substr(0,2);
      if(vcity[province] == undefined)
      {
        return false;
      }
      else
        {
          return true;
        }
    };

//檢查生日是否正確
    $scope.checkBirthday = function(card)
    {
      var len = card.length;
      //身份證15位時,次序為省(3位)市(3位)年(2位)月(2位)日(2位)校驗位(3位),皆為數字
      if(len == '15')
      {
        var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/;
        var arr_data = card.match(re_fifteen);
        var year = arr_data[2];
        var month = arr_data[3];
        var day = arr_data[4];
        var birthday = new Date('19'+year+'/'+month+'/'+day);
        return $scope.verifyBirthday('19'+year,month,day,birthday);
      }
      //身份證18位時,次序為省(3位)市(3位)年(4位)月(2位)日(2位)校驗位(4位),校驗位末尾可能為X
      if(len == '18')
      {
        var re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/;
        var arr_data = card.match(re_eighteen);
        var year = arr_data[2];
        var month = arr_data[3];
        var day = arr_data[4];
        var birthday = new Date(year+'/'+month+'/'+day);
        return $scope.verifyBirthday(year,month,day,birthday);
      }
      return false;
    };

//校驗日期
    $scope.verifyBirthday = function(year,month,day,birthday)
    {
      var now = new Date();
      var now_year = now.getFullYear();
      //年月日是否合理
      if(birthday.getFullYear() == year && (birthday.getMonth() + 1) == month && birthday.getDate() == day)
      {
        //判斷年份的範圍(3歲到100歲之間)
        var time = now_year - year;
        if(time >= 3 && time <= 100)
        {
          return true;
        }
        return false;
      }
      return false;
    };

//校驗位的檢測
    $scope.checkParity = function(card)
    {
      //15位轉18位
      card = $scope.changeFivteenToEighteen(card);
      var len = card.length;
      if(len == '18')
      {
        var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
        var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
        var cardTemp = 0, i, valnum;
        for(i = 0; i < 17; i ++)
        {
          cardTemp += card.substr(i, 1) * arrInt[i];
        }
        valnum = arrCh[cardTemp % 11];
        if (valnum == card.substr(17, 1))
        {
          return true;
        }
        return false;
      }
      return false;
    };

//15位轉18位身份證號
    $scope.changeFivteenToEighteen = function(card)
    {
      if(card.length == '15')
      {
        var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
        var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
        var cardTemp = 0, i;
        card = card.substr(0, 6) + '19' + card.substr(6, card.length - 6);
        for(i = 0; i < 17; i ++)
        {
          cardTemp += card.substr(i, 1) * arrInt[i];
        }
        card += arrCh[cardTemp % 11];
        return card;
      }
      return card;
    };

    $scope.emailCheck = function(){
      if($scope.selectedMailMsg.book_user_email != null){
        var pattern = /^([\.a-zA-Z0-9_-])
[email protected]
([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/; if(!pattern.test($scope.selectedMailMsg.book_user_email)){ $scope.selectedMailMsg.book_user_email = null; $ionicLoading.show({ template: "請輸入正確的郵箱格式" }); $timeout(function(){ $ionicLoading.hide(); },2000); } } }; $scope.goOrderPayPage = function(){ $scope.btnDisabled = true; $scope.telNumCheck(); $scope.personNumCheck(); $scope.emailCheck(); if(($scope.selectedMailMsg.book_user == null) || ($scope.selectedMailMsg.book_user_tel == null) || ($scope.selectedMailMsg.book_user_idcard == null) || ($scope.selectedMailMsg.book_user_email == null)){ $ionicLoading.show({ template: "請完善個人資訊" }); $timeout(function(){ $ionicLoading.hide(); },2000); $scope.btnDisabled = false; }else{ $scope.btnDisabled = true; $scope.startDate = $scope.startDateForShow; $scope.endDate = $scope.endDateForShow; $scope.book_start_date = $filter('date')($scope.startDateForShow,'yyyy-MM-dd') + " 12:00:00"; $scope.book_end_date = $filter('date')($scope.endDateForShow,'yyyy-MM-dd') + " 12:00:00"; if ($rootScope.user.hotelOnlinePay == 1) { houseService.addHotelOrderDetail({"goods_id":$scope.goods_id ,"detail_state":1 ,"book_user":$scope.selectedMailMsg.book_user ,"book_user_tel":$scope.selectedMailMsg.book_user_tel ,"book_user_idcard":$scope.selectedMailMsg.book_user_idcard ,"book_user_email":$scope.selectedMailMsg.book_user_email ,"bookdate_lst":$scope.bookdate_lst ,"book_totalprice":Number($scope.book_unitprice) ,"book_start_date":$scope.book_start_date ,"book_end_date":$scope.book_end_date ,"airbnb_id":$scope.curAirbnbId},getHotelOrderDetailResult,getHotelOrderDetailFalse); } else { houseService.onlinePayHotelPreOrder({"goods_id":$scope.goods_id ,"detail_state":1 ,"book_user":$scope.selectedMailMsg.book_user ,"book_user_tel":$scope.selectedMailMsg.book_user_tel ,"book_user_idcard":$scope.selectedMailMsg.book_user_idcard ,"book_user_email":$scope.selectedMailMsg.book_user_email ,"bookdate_lst":$scope.bookdate_lst ,"book_totalprice":Number($scope.book_unitprice) ,"book_start_date":$scope.book_start_date ,"book_end_date":$scope.book_end_date ,"airbnb_id":$scope.curAirbnbId},getHotelOrderDetailResult,getHotelOrderDetailFalse); } } }; function getHotelOrderDetailResult(resp){ $scope.book_detail_id = resp.book_detail_id; $scope.book_serial_num = resp.book_serial_num; if ($rootScope.user.hotelOnlinePay == 1) { $state.go('orderPay',{"goodsDetailIdString":$scope.book_detail_id,"priceResult":$scope.book_unitprice,"book_serial_num":$scope.book_serial_num}); $ionicLoading.show({ template: "訂單提交成功,正在跳轉至支付介面。。。" }); $timeout(function(){ $ionicLoading.hide(); },2000); } else { $ionicLoading.show({ template: "預約成功,正在跳轉至訂單介面。。。" }); $rootScope.orderType = -1; $timeout(function(){ $ionicLoading.hide(); $state.go('indexTab.order'); },2000); } } function getHotelOrderDetailFalse(data){ $scope.btnDisabled = false; if(data == undefined){ $ionicLoading.show({ template: "網路連線不給力" }); $timeout(function(){ $ionicLoading.hide(); },2000); }else{ $ionicLoading.show({ template: "該民宿已經被預訂" }); $timeout(function(){ $ionicLoading.hide(); },2000); } } $scope.selectedMailMsg = []; $scope.selectedMailId = $rootScope.defaultUserId; if($scope.selectedMailId != undefined) { mineService.getContactUserInfo({"contract_id": $scope.selectedMailId}, getMailInfoSuccess); } function getMailInfoSuccess(resp){ $scope.selectedMailMsg = resp; } $ionicModal.fromTemplateUrl('templates/selectedMailModal.html', { scope: $scope, animation: 'slide-in-up' }).then(function(modal) { $scope.selectedMailModal = modal; }); $scope.selectedTheBookUser = function(){ $scope.selectedMailModal.show(); mineService.queryContactUserInfo(getMailListSuccess); }; $scope.closeModalForSelectedMail = function(){ $scope.selectedMailModal.hide(); }; function getMailListSuccess(resp){ $scope.defaultLoad = true; $scope.mailListData = resp.contact_lst; angular.forEach($scope.mailListData,function(data){ if(data.contract_id == $scope.selectedMailId){ data.selected = true; }else{ data.selected = false; } }); } $scope.mailSelected = function(item){ $scope.selectedMailId = item.contract_id; angular.forEach($scope.mailListData,function(data){ data.selected = false; }); item.selected = true; mineService.getContactUserInfo({"contract_id":$scope.selectedMailId},getMailInfoSuccess); $scope.closeModalForSelectedMail(); }; $scope.$on('$destroy',function(){ $scope.selectedMailModal.hide(); }) } })();