正則表達式:
@input="form.userName = form.userName.replace(/\s+/g,'')" ( 禁止輸入空格)
@input="form.phone = form.phone.replace(/[^\d]/g, '')" ( 只能輸入數字 )
@input="form.name = form.name.replace(/[^\u4e00-\u9fa5]/g, '')" (禁止輸入數字)
var reg = /^[_a-zA-Z0-9]/ //匹配 下划線 英語字母 或者 數字開頭,滿足其中之一
// 校驗 input 只能輸入小數點后一位 =》 this.updateData.scoreCeil 是你要保留小數的 字段
this.updateData.scoreCeil = this.updateData.scoreCeil.replace(/[^\d.]/g,""); //清除“數字”和“.”以外的字符
this.updateData.scoreCeil = this.updateData.scoreCeil.replace(/\.{2,}/g,"."); //只保留第一個. 清除多余的
this.updateData.scoreCeil = this.updateData.scoreCeil.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
this.updateData.scoreCeil = this.updateData.scoreCeil.replace(/^(\-)*(\d+)\.(\d).*$/,'$1$2.$3');//只能輸入兩個小數 \d 代表一個小數, \d\d 代表保留倆位小數
if(this.updateData.scoreCeil.indexOf(".")< 0 && this.updateData.scoreCeil !=""){//以上已經過濾,此處控制的是如果沒有小數點,首位不能為類似於 01、02的金額
this.updateData.scoreCeil= parseFloat(this.updateData.scoreCeil);
}
備注: this.addData.content 是 富文本框的綁定的參數,不需要做任何處理。
var contents = ''
contents = this.addData.content.replace(/<[^>]+>/g, "")
if( contents == '' ){
this.$message.error("留言不能為空")
return
}
if(contents.length < 10 ){
this.$message.error("留言字數不能小於十個字")
return
}
if( contents.length < 10 ){
if( contents.indexOf(',') != -1 || this.addData.content.indexOf(',') != -1 ){
this.$message.error("輸入字數十個字的時候不能包含逗號")
return
}
}
var contents2 = /([^u4E00-u9FA5])\1{2}/g; //不能連續輸入三個相同的字符,包括大小寫英文字母
if( contents.toLowerCase().match(contents2) != null ){
this.$message.error("不能連續輸入三個相同的字符")
return
}
只能數字加英文
<input @input="value=value.replace(/[^\w\.\/]/ig,'')"> //包含點 <input @input="value=value.replace(/[^\w\/]/ig,'')"> //不包含點
// 去除特殊符號,包括空格
var contents11 = 放你自己的變量.replace(/[`~!@#$^\-&*()=|{}':;',\\\[\]\.<>\/?~!@#¥……&*()——|{}【】';:""'。,、?\s]/g, "");
//身份證校驗
var cardRule = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
