js 校验问题

来源:互联网 发布:ipad air2卸载软件 编辑:程序博客网 时间:2024/04/29 18:38

1、校验双字符单字符长度

var getBLen = function(str) {  if (str == null) return 0;  if (typeof str != "string"){    str += "";  }  return str.replace(/[^x00-xff]/g,"01").length;}eg:var custNameLen = getBLen(Trim(custName,'g'));    if(custNameLen > 20) {        mecv.message.show({              title:"提示信息",              body: "客户名称过长,请重新输入!",              cancel:false,              buttons:[{name:'确定',sid:"ensure"}]            });        return false;    }

2、去掉字符串中所有空格(包括中间空格,需要设置第2个参数为:g)
function Trim(str,is_global){    var result;    result = str.replace(/(^\s+)|(\s+$)/g,"");    if(is_global.toLowerCase()=="g"){        result = result.replace(/\s/g,"");    }    return result;}

3、毫秒格式化日期数据
// time:毫秒,format:日期格式function format(time, format){var t = new Date(time);var tf = function(i){return (i < 10 ? '0' : '') + i};return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){switch(a){case 'yyyy':return tf(t.getFullYear());break;case 'MM':return tf(t.getMonth() + 1);break;case 'mm':return tf(t.getMinutes());break;case 'dd':return tf(t.getDate());break;case 'HH':return tf(t.getHours());break;case 'ss':return tf(t.getSeconds());break;};});}eg:format(item.planDeliveryDate, 'yyyy/MM/dd HH:mm:ss')



































0 0