angular写的身份证校验规则(省份生日性别)手机号校验 。直接贴代码

3 篇文章 0 订阅
1 篇文章 0 订阅
(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_-])+@([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();
    })
  }
})();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值