年月-输入格式的判断

工作中常遇到年份要限定4位数,或者月份小于10要补充“0”,并检测月份在1-12月份之间:

function init() {

// var riQiList=[];

$.each(mediForm.fldCtrls,function (key,ctrls){

if(key.indexOf('faBing')==0||key.indexOf('part9_time')==0){

var timeCtrl=mediForm.fldCtrls[key];

var $timeCnt=timeCtrl.$container;

$timeCnt.find("div[data-subkey='year']").append("</br><span class='diy-error-msg' style='color:#ff4081;display:none;'>请输入4位年份</span>");

$timeCnt.find("div[data-subkey='month']").append("</br><span class='diy-error-msg' style='color:#ff4081;display:none;'>请输入01-12月份</span>");

if(timeCtrl){//显示时

checkMonth(timeCtrl.koValue().month(),timeCtrl);

checkYear(timeCtrl.koValue().year(),timeCtrl);

//这是knockout.js中KO绑定subscribe订阅时

Knockout是建立在以下三大核心功能之上的:

  1. 监控属性和依赖跟踪(Observables and dependency tracking)
  2. 声明式绑定(Declarative bindings)
  3. 模板(Templating)

timeCtrl.koValue().month.subscribe(function(newValue){

checkMonth(newValue,timeCtrl)

})

timeCtrl.koValue().year.subscribe(function(newValue){

checkYear(newValue,timeCtrl)

})

}

}

})

}

// 年月-输入格式的判断 核心代码:

function checkMonth(val,nowCtrl){

var $nowCnt=nowCtrl.$container;

版本1:(不严谨,未考虑到用户会输入0)

if(val){

$nowCnt.find("div[data-subkey='month']").find("span.diy-error-msg").hide();

if (val>=1 && val<10) {

val='0'+parseInt(val);

$nowCnt.find("div[data-subkey='month']").find("span.diy-error-msg").hide();

if(isPhone==false){

$nowCnt.find('div[data-subKey="month"] input').val(val)

}else{

$nowCnt.find('div[data-subKey="month"] span').first().text(val);

}

}else if (val>12) {

$nowCnt.find("div[data-subkey='month']").find("span.diy-error-msg").show();

}

}else{

$nowCnt.find("div[data-subkey='month']").find("span.diy-error-msg").hide();

}

}

修改为:

function checkMonth(val,nowCtrl){

var $nowCnt=nowCtrl.$container;

if(val){

$nowCnt.find("div[data-subkey='month']").find("span.diy-error-msg").hide();

if(isPhone==false){

$nowCnt.find('div[data-subKey="month"] input').val(val)

}else{

$nowCnt.find('div[data-subKey="month"] span').first().text(val);

}

if (val>=1 && val<10) {

val='0'+parseInt(val);

$nowCnt.find("div[data-subkey='month']").find("span.diy-error-msg").hide();

if(isPhone==false){

$nowCnt.find('div[data-subKey="month"] input').val(val)

}else{

$nowCnt.find('div[data-subKey="month"] span').first().text(val);

}

}else if(val>12||val<=0){

$nowCnt.find("div[data-subkey='month']").find("span.diy-error-msg").show();

}

}else{

$nowCnt.find("div[data-subkey='month']").find("span.diy-error-msg").hide();

}

}

function checkYear(newValue,nowCtrl){

var $nowCnt=nowCtrl.$container;

年份的判断主要是限定4位数的正则表达式:

var reg=/^\d{4}$/ ;

if(newValue){

if(!reg.test(newValue)){

$nowCnt.find("div[data-subkey='year']").find("span.diy-error-msg").show();

}else{

$nowCnt.find("div[data-subkey='year']").find("span.diy-error-msg").hide();

}

}else{

$nowCnt.find("div[data-subkey='year']").find("span.diy-error-msg").hide();

}

}

//save函数,在保存时执行

function save(){

// debugger;

var num=0;

$.each(mediForm.fldCtrls,function (key,ctrls){

if(key.indexOf('faBing')==0||key.indexOf('part9_time')==0){

var timeCtrl=mediForm.fldCtrls[key];

var $timeCnt=timeCtrl.$container;

var objChild=["year","month"];

$.each(objChild,function(idx,zhi){

var $childCnt=$timeCnt.find("div[data-subkey="+zhi+"]");

var cls=$childCnt.find("span.diy-error-msg").css("display");

if(cls){ //none ||inline

if(cls!=='none'){

num++;

}

}

})

}

})

if(num>0){

return {error:'格式有误'}

}else {

return true;

}

}

猜你喜欢

转载自blog.csdn.net/weixin_42065612/article/details/88951337