vue input 自定义组件化 只能输入 整数 小数点两位 最大值 最大位数
d-input.vue
export default {
name: 'd-input',
props :{
value: {
// type: String
},
placeholder:{
type: String
},
maxLength:{
type: String
},
type:{
default:'text',
type: String
},
maxStr:{
// type: String,
// default:''
},
minStr:{
},
disabled:{
type:Boolean,
default:false
}
},
data () {
return {
// model:this.models
}
},
watch: {
value(data){
// console.log(data)
}
},
methods:{
// .substr(0,$event.srcElement.value.length - 1);吃掉最后一位
inputChange( $event ) {
this.model = $event.srcElement.value
if( this.type == 'int' ) {
if (this.model < 0) {
return $event.srcElement.value = ''
}
if( this.model && this.model.length > this.maxLength ) {
this.model = this.model.slice( 0,this.maxLength );
}
$event.srcElement.value = this.model == '' ? '' : Number( parseInt(this.model)).toString() != 'NaN' ? Number( parseInt(this.model)).toString() : '';
// if( parseFloat( $event ) > parseFloat( this.maxStr ) ) {
// $event = ( parseInt( $event/10 ) )
// }
} else if( this.type == 'float' ) {
$event.srcElement.value = Number($event.srcElement.value.match( /^\d*(\.?\d{0,2})/g)[0]).toString() != 'NaN'?$event.srcElement.value.match( /^\d*(\.?\d{0,2})/g)[0]:''
// if( parseFloat( $event ) >= parseFloat( this.maxStr ) ) {
// $event = ( $event/10 ).toString().match( /^\d*(\.?\d{0,2})/g)[0]
// }
let str_before = $event.srcElement.value.split(0)[0]
let str_d = $event.srcElement.value.split(0)[1]
if( $event.srcElement.value&&$event.srcElement.value.length>1 && str_before == ''&& str_d.indexOf('.') == '-1'){
$event.srcElement.value = $event.srcElement.value.split(0)[1]
}
}else
if(this.type === 'text')
{
let reg = /[`~!@#$%^&*()_\-+=<>?:"{}|,.\/;'\\[\]·~!@#¥%……&*()——\-+={}|《》?:“”【】、;‘’,。、]/im;
if(reg.test($event.target.value)){
$event.target.value = $event.target.value.substr(0,$event.target.value.length - 1);
}
}
if( parseInt( $event.srcElement.value ) > parseFloat( this.maxStr ) ) {
$event.srcElement.value = ($event.srcElement.value.substr(0,$event.srcElement.value.length - 1)) == ''?'':Number($event.srcElement.value.substr(0,$event.srcElement.value.length - 1))
//大于maxStr 阻止输入 吃掉最后一位
}
this.model = $event.srcElement.value
this.$emit('input',$event.srcElement.value)
this.$emit('change',$event.srcElement.value)
}
},
}
.addDiscount section .addDiscount-form .addDiscount-form-max-inputs {
width: 430px;
margin-right: 10px;
}
.el-inputs {
position: relative;
font-size: 14px;
display: inline-block;
width: 100%;
}
.el-input__inners{
background-color: #FFF;
color: #606266;
position: relative;
font-size: 14px;
padding: 0 8px;
display: inline-block;
width: 100%;
height: 40px;
line-height: 40px;
outline: 0;
border-radius:5px;
border: 1px solid #DCDFE6;
box-sizing: border-box;
transition: border-color .2s cubic-bezier(.645,.045,.355,1);
-webkit-appearance: none;
}
input::-webkit-input-placeholder{
color:#DCDFE6;
}
input::-moz-placeholder{ /* Mozilla Firefox 19+ */
color:#DCDFE6;
}
input:-moz-placeholder{ /* Mozilla Firefox 4 to 18 */
color:#DCDFE6;
}
input:-ms-input-placeholder{ /* Internet Explorer 10-11 */
color:#DCDFE6;
}
.opoUp{
background: rgba(0,0,0,0.03)
}
.el-input__inner{
padding: 0 5px;
}
引用
import dInput from "@/components/d-input";
components: {
dInput
},
maxLength="6" type="int">