el-input中,输入input和失去焦点blur时进行校验——正则表达式之只能输入数字和大写字母 & 只能输入数字 & 颜色值的正则表达式-3位和6位 & 标签的回车事件

el-input中,输入input和失去焦点blur时进行校验——正则表达式之只能输入数字和大写字母 & 只能输入数字 & 颜色值的正则表达式-3位和6位 & 标签的回车事件

html

<el-row class="normal-row-class">
    <el-col :span="8" class='name-label'>线路渲染颜色:el-col>
    <el-col :span="16">
        <el-input :disabled="showColor" v-model="editObj[editObj.objName].COLOR" @input='colorValue(editObj[editObj.objName].COLOR,false)' @blur ='colorValue(editObj[editObj.objName].COLOR,true)'>el-input>
    el-col>
el-row>

js

methods:{
     /**
     * @description: 线路渲染颜色输入——输入&失去焦点时进行校验,超出6位进行截取
     * @param {String} val 线路渲染颜色value值
     * @param {Number} isTips false-输入时 true-失去焦点时
     */
    colorValue(value,isTips) {
      this.editObj[this.editObj.objName].COLOR = value.replace(/[^A-Z0-9]/g, '')
      if(value.length >= 6){
        this.editObj[this.editObj.objName].COLOR = value.slice(0,6) 
      }
      if(value.length < 6 && isTips){
        this.$message.error('线路渲染颜色应为6位数字或大写字母,请重新输入');
        this.editObj[this.editObj.objName].COLOR = ''
      }
    },
}
input标签只能输入数字
只允许输入数字(整数:小数点不能输入)
<input type="text" onkeyup="value=value.replace(/[^\d]/g,'')" > 
允许输入小数(两位小数)
<input type="text" onkeyup="value=value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')" > 
允许输入小数(一位小数)
<input type="text" onkeyup="value=value.replace(/^\D*(\d*(?:\.\d{0,1})?).*$/g, '$1')" > 
开头不能为0,且不能输入小数
<input type="text" onkeyup="value=value.replace(/[^\d]/g,'').replace(/^0{1,}/g,'')" >
标签的回车事件
  • @keyup.enter.native=‘getData()’
<el-input v-model="name" @keyup.enter.native="onSubmit" placeholder="User Name">el-input>
附1:十六进制颜色值的正则表达式
let colorHex = "#FF00FF";
const reg = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
alert(reg.test(colorHex))
附2:正则集合

<input type="text" onkeyup="value=value.replace(/[^\d]/g,'')">
 

<input type="text" onkeyup="this.value=this.value.replace(/\D/g,'')">  
  

<input type="text" onkeyup="value=value.replace(/[^\d.]/g,'')">
 

<input onKeyUp="value=value.replace(/[\W]/g,'')">  
 

<input onKeyUp="value=value.replace(/[^A-Z0-9]/g, '')">  
 

<input type="text" onkeyup="this.value=this.value.replace(/[^\w_]/g,'');"> 
 

<input type="text" onkeyup="this.value=this.value.replace(/[^a-z0-9_]/g,'');"> 
 

<input type="text" onkeyup="this.value=this.value.replace(/[^\u4e00-\u9fa5]/g,'')">  
  
 
<input type="text" onkeyup="this.value=this.value.replace(/[^a-zA-Z]/g,'')">  
  
 
<input onkeyup="value=value.replace(/[^\w\u4E00-\u9FA5]/g, '')">  
  

<input type="text" onkeyup="this.value=this.value.replace(/^[^!@#$%^&*()-=+]/g,'')">
 

<input onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')">
 

<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')">
<input name=txt1 onchange="if(/\D/.test(this.value)){alert('只能输入数字');this.value='';}">
 

<input type=text t_value="" o_value="" onkeypress="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onkeyup="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onblur="if(!this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?|\.\d*?)?$/))this.value=this.o_value;else{if(this.value.match(/^\.\d+$/))this.value=0+this.value;if(this.value.match(/^\.$/))this.value=0;this.o_value=this.value}">
 

<input onkeyup="value=value.replace(/[\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[\d]/g,''))" maxlength=10 name="Numbers">
 

<input onkeyup="value=value.replace(/[^\w\.\/]/ig,'')">
 

<input onKeyUp="value=value.replace(/[^\d|chun]/g,'')">
 

<input onKeyPress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 || /\.\d\d$/.test(value))event.returnValue=false">
 

<input onkeyup="this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')">

你可能感兴趣的:(js知识,正则表达式,前端,javascript)