1. 程式人生 > >項目中用到的正則

項目中用到的正則

str 所有 input reg 格式 == pan get function

一、小數

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(""); } } }

項目中用到的正則