项目中用到的正则

一、小数

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

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

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

str.replace(/[^a-zA-0-9u4E00-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("");
        }
    }
}
--by 驻北静望
原文地址:https://www.cnblogs.com/web520/p/8023925.html