angular常用directive,身份证号码,倒计时按钮,重复值,input后端校验


/**
* Created by jing on 14-11-4.
*/
angular.module('app')
.directive('jingIdCard', function () {
/**
* 身份证号码验证
*
* @param cardNo
* {String} 证件号码
* @returns info {Object} 身份证信息.
*
*/
var getIdCardInfo = function (cardNo) {
var info = {
isTrue: false, // 身份证号是否有效。默认为 false
year: null,// 出生年。默认为null
month: null,// 出生月。默认为null
day: null,// 出生日。默认为null
isMale: false,// 是否为男性。默认false
isFemale: false // 是否为女性。默认false
};

if (!cardNo && 15 != cardNo.length && 18 != cardNo.length) {
info.isTrue = false;
return info;
}

if (15 == cardNo.length) {
var year = cardNo.substring(6, 8);
var month = cardNo.substring(8, 10);
var day = cardNo.substring(10, 12);
var p = cardNo.substring(14, 15); // 性别位
var birthday = new Date(year, parseFloat(month) - 1, parseFloat(day));
// 对于老身份证中的年龄则不需考虑千年虫问题而使用getYear()方法
if (birthday.getYear() != parseFloat(year)
|| birthday.getMonth() != parseFloat(month) - 1
|| birthday.getDate() != parseFloat(day)) {
info.isTrue = false;
} else {
info.isTrue = true;
info.year = birthday.getFullYear();
info.month = birthday.getMonth() + 1;
info.day = birthday.getDate();
if (p % 2 == 0) {
info.isFemale = true;
info.isMale = false;
} else {
info.isFemale = false;
info.isMale = true;
}
}
return info;
}

if (18 == cardNo.length) {
var year = cardNo.substring(6, 10);
var month = cardNo.substring(10, 12);
var day = cardNo.substring(12, 14);
var p = cardNo.substring(14, 17);
var birthday = new Date(year, parseFloat(month) - 1, parseFloat(day));
// 这里用getFullYear()获取年份,避免千年虫问题
if (birthday.getFullYear() != parseFloat(year)
|| birthday.getMonth() != parseFloat(month) - 1
|| birthday.getDate() != parseFloat(day)) {
info.isTrue = false;
return info;
}

var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1];// 加权因子
var Y = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2];// 身份证验证位值.10代表X

// 验证校验位
var sum = 0; // 声明加权求和变量
var _cardNo = cardNo.split("");

if (_cardNo[17].toLowerCase() == 'x') {
_cardNo[17] = 10;// 将最后位为x的验证码替换为10方便后续操作
}
for (var i = 0; i < 17; i++) {
sum += Wi[i] * _cardNo[i];// 加权求和
}
var i = sum % 11;// 得到验证码所位置

if (_cardNo[17] != Y[i]) {
return info.isTrue = false;
}

info.isTrue = true;
info.year = birthday.getFullYear();
info.month = birthday.getMonth() + 1;
info.day = birthday.getDate();

if (p % 2 == 0) {
info.isFemale = true;
info.isMale = false;
} else {
info.isFemale = false;
info.isMale = true;
}
return info;
}
return info;
}
return {
require: 'ngModel',
link: function (scope, ele, attrs, c) {
scope.$watch(attrs.ngModel, function (n) {
if (!n) return;
var cardInfo = getIdCardInfo(n);
c.$setValidity('isIdCard', cardInfo.isTrue);
});
}
}

})
.directive('jingTimeButton', function ($interval) {

function link(scope, ele, attrs, c) {
var timeoutId;
var seconds;
var txt = ele.text();
ele.on('click', function () {
seconds = parseInt(attrs.jingSeconds) || 60;
ele.attr('disabled', 'disabled');
scope.$apply(scope.jingAction);
timeoutId = $interval(function () {
seconds--;
if (seconds >= 0) {
ele.text(txt + '(' + seconds + ')');
} else {
stopF();
ele.removeAttr('disabled');
ele.prop('disabled', false);
}
}, 1000);
ele.on('$destroy', function () {
stopF();
});
});

function stopF() {
if (angular.isDefined(timeoutId)) {
$interval.cancel(timeoutId);
timeoutId = undefined;
}
}
}

return {
scope: {jingAction: '&'},
link: link
}
})

.directive('jingEqualTo', function () {
return {
restrict: "A",
require: 'ngModel',
link: function (scope, element, attrs, ctrl) {
var tageCtrl = scope.$eval(attrs.jingEqualTo);
tageCtrl.$parsers.push(function (viewValue) {
ctrl.$setValidity('equalto', viewValue == ctrl.$viewValue);
return viewValue;
});
ctrl.$parsers.push(function (viewValue) {
if (viewValue == tageCtrl.$viewValue) {
ctrl.$setValidity('equalto', true);
return viewValue;
} else {
ctrl.$setValidity('equalto', false);
return undefined;
}
});
}
};
})
.directive('jingBtcAddress', function (Http, $timeout) {
return {
require: 'ngModel',
link: function (scope, ele, attrs, c) {
var timeout;
scope.$watch(attrs.ngModel, function (n) {
if (timeout) $timeout.cancel(timeout);
if (!n) return;
timeout = $timeout(function () {
Http.get('btc/checkAddress/' + n).success(function (data) {
c.$setValidity('isBtcAddress', data.checked);
}).error(function (data) {
c.$setValidity('isBtcAddress', false);
});
}, 600);
});
}
};
});
;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值