码迷,mamicode.com
首页 > 其他好文 > 详细

项目中用到的正则

时间:2017-12-11 17:11:43      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:button   his   color   text   swa   reg   nbsp   log   title   

一、小数

str.replace(/[^\d.]+/g,‘‘).replace(‘.‘,‘$#$‘).replace(/\./g,‘‘).replace(‘$#$‘,‘.‘).replace(/^\./g,‘‘)

将所有不是数字和小数点的置空,将第一个小数点变为$#$,将所有小数点置空,将$#$变为小数点,将首位小数点置空

二、只能是字母数字和汉字

str.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5]/g,‘‘)

三、只能是数字

str.value.replace(/\D/g,‘‘)

四、日期格式

<input type=‘text‘ onkeyup=‘checkDate(this.value,jQuery(this))‘ onblur=‘blurdate(this.value,jQuery(this))‘ >

function checkDate(date,a){
    if(date.length==10){
        var reg = /^(\d{1,4})(-)(\d{1,2})\2(\d{1,2})$/;
        var r = date.match(reg);
        if(r==null){
            swal({
                title: "您输入的日期格式不正确!",
                timer: 1300,
                type:"warning",
                showConfirmButton: false
            });
            a.val("")
        }else{
            var d = new Date(r[1], r[3] - 1, r[4]);
            var c=(d.getFullYear() == r[1] && (d.getMonth() + 1) == re[3] && d.getDate() == r[4]);
            if(!c){
                swal({
                    title: "请输入正确的日期!",
                    timer: 1300,
                    type:"warning",
                    showConfirmButton: false
                });
                a.val("");
            }
        }
    }
}
function blurdate(date,a){
    if(date!=""){
       if(date.length<10){
            swal({
                    title: "您输入的日期格式不正确!",
                    timer: 1300,
                    type:"warning",
                    showConfirmButton: false
                });
            a.val("")
            a.parent().parent().find("input[type=hidden]").val("");
        }
    }
}

 

项目中用到的正则

标签:button   his   color   text   swa   reg   nbsp   log   title   

原文地址:http://www.cnblogs.com/web520/p/8023925.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!