I had some problems writing code today ,input of type Attribute value is number Hour ,maxlength The setting of will be invalid ,type Attribute value is text Hour , Can't satisfy input
Limit of number only , The solutions found are as follows :
1, Only numbers and decimal points can be entered
@keyup="val.num = val.num.replace(/[^\d.]/g,'')"
concrete content :
<form v-for="(val,index) in list" :key='val.id'> <input type = "text"
placeholder = " Please enter quantity " v-model = "val.num" maxlength = "7" @keyup = "val.num =
val.num.replace(/[^\d.]/g,'')" /> </form>
2, Only one decimal point can be entered , When the second is not '.' Hour , First cannot be 0
2.1 Limit one decimal point
val.num= val.num .toString().//replace() Method is a string method .replace(/\.{2,}/g,'.') .
replace('.','$#$') .replace(/\./g,'') .replace('$#$','.')// Only one decimal point can be entered
2.2 First cannot be 0
if(val.num.length > 1 && val.num.substr(0,1) ==0 && val.num.substr(1,1) !='.'){
val.num= val.num.substr(1,1) }
2.3, The first is not a point
Method 1 :
if(val.num.substr(0,1) == '.'){ val.num="" }
Method II :
if(val.num.indexOf(".") <= 0 && val.num!= ''){ val.num= parseFloat(val.num) }
2.4, Limit the number of decimal places (3 Bit as an example )
val.num= val.num .toString() .replace(/^()*(\d+)\.(\.?\d\d\d).*$/,'$2$2.$3')
//d There are several decimal places
Technology
Daily Recommendation