How to realize the Segmentation input Box of Mobile phone number by vue+iview
This article mainly explains the "vue+iview how to achieve mobile phone number segment input box", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "vue+iview how to achieve mobile phone number segment input box" bar!
The details are as follows
Export default {data () {return {required: this.$attrs.hasOwnProperty ('required'), phoneLength: 11, phoneReg: / ^ (13 [0-9] | 14 [579] | 15 [0-3 5-9] | 16 [6] | 17 [0135678] | 18 [0-9] | 19 [89])\ d {8} $/, numberRefName:' numberBlock', validTimer: null, dirty: false, valid: false Validated: false,} }, methods: {handlerFocus () {if (! this.dirty) {this.dirty = this.required? True: false;}}, handlerInput (e, index) {if (! e.target.value) {return;} this.dirty = true; let value = e.target.value.replace (/\ Dogma g,''); value = value? Value [0]:'; / / legal value, switch to the next input box if (value.length) {this.changeInput (index + 1);} / / # end e.target.value = value; this.debounceValidate ();}, changeInput (index) {if (index)
< 0 || index === this.phoneLength) return; const target = this.$refs[this.numberRefName + index][0]; target.focus(); if (target.value && target.setSelectionRange) { target.setSelectionRange(1, 1);//maxlength="1" 时无效,所以去掉了... } }, deleteNumber(e, index) { if (e.target.value) { e.target.value = '' } else { this.changeInput(index - 1); } }, resetStatus() { this.validated = false; this.dirty = false; }, cleanValue() { this.resetStatus(); const numberBlocks = this.$refs; for (let i in numberBlocks) { numberBlocks[i][0].value = ''; } if (this.required) { const FormItem = this.getFormItem(); if (FormItem) { FormItem.resetField(); FormItem.$emit('on-form-change', null); } } // this.changeInput(0); }, debounceValidate() { this.validTimer = setTimeout(() =>{this.validate ();}, 300);}, validate (isLeave) {const numberBlocks = this.$refs; let result =''; for (let i in numberBlocks) {result + = numberBlocks [I] [0] .value;} if (result.length = this.phoneLength | | isLeave) {this.validated = true This.dispath ({value: result, valid: this.valid = this.phoneReg.test (result),});}}, dispath (info) {this.$emit ('input', info.valid? Info.value:''); if (this.required) {const FormItem = this.getFormItem (); if (FormItem) {this.updateFormItem (FormItem, info.valid? Info.value:'');}, getFormItem () {let MAX_LEVEL = 3; let parent = this.$parent; let name = parent.$options.name; while (MAX_LEVEL & & name! = = 'FormItem') {MAX_LEVEL--; if (! parent) return null; parent = parent.$parent } return parent | | null;}, updateFormItem (FormItem, data) {FormItem.$emit ('on-form-change', data);}, pageEvent () {if (this.dirty) {this.validate (true);}},}, created () {window.addEventListener (' click', this.pageEvent) }, beforeDestroy () {window.removeEventListener ('click', this.pageEvent);},}; .ivu-phone-input {.clean-btn {transition: opacity .5s; opacity: 0; cursor: pointer;} &: hover {.clean-btn {opacity: 1;}} .number-block {display: inline-block; padding: 0 Height: 30px; width: 28px; text-align: center; margin-right: 2px; &: nth-child (3) {margin-right: 10px;} &: nth-child (7) {margin-right: 10px }} Thank you for your reading. The above is the content of "how to achieve the segmented input box of mobile phone number in vue+iview". After the study of this article, I believe you have a deeper understanding of how to realize the segmented input box of mobile phone number in vue+iview, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!