This article is to share with you the simple realization of the synchronous display function of mobile phone number and bank card. Interested friends can take a look at
This is an interview question from a certain alliance
Isn’t it all about having a clear idea and then optimizing it while writing
At that time, I said that thinking should be calculated The attribute calculates the value of the preview box based on the content of the input box.
Just handle the focus and blur events.
You have to write by hand. I can’t write by hand. Then the interviewer thought that I would be speechless.
The length of the input content in the input box is greater than 0, and the preview information is displayed
Leave the cursor and close the preview information
Insert a special character_ every 4 digits in the preview information, and the input content remains unchanged
Just use the calculated attribute to judge
<template lang="html"> <p class="zInput"> <p class="big-show" v-show="showBig">{{txt2}}</p> <input type="text" @blur="handerBlur" @focus="handerFocus" v-model="txt"> </p> </template> <script> export default { name: 'z-input', data () { return { txt: '', showBig: false, } }, computed: { txt2: function () { if (this.txt.length > 0) { this.showBig = true } return this.getStr(this.txt, 4) } }, methods: { handerBlur () { this.showBig = false }, handerFocus (evt) { if (evt.target.value.length > 0) { this.showBig = true } }, getStr (str, len) { let lenth = str.length let len1 = len - 1 let newStr = '' for (var i = 0; i < lenth; i++) { if (i % len === len1 && i > 0) { newStr += str.charAt(i) + '_' } else { newStr += str.charAt(i) } } if (newStr.length % (len + 1) === 0) { // 解决最后一位为补充项问题 newStr = newStr.substr(0, newStr.length - 1) } return newStr }, } } </script> <style lang="less"> .zInput{ position: relative; .big-show { position: absolute; top: -40px; font-size: 36px; line-height: 40px; background-color: red; } } .zInput{ top:50px; } </style>
If another length limit is added, the above method is not suitable. The replacement implementation plan v-model
is actually a syntactic sugar
written separately as v-bind:value
andv-on:input
<template lang="html"> <p class="zInput"> <p class="big-show" v-show="showBig">{{txt2}}</p> <input type="text" @blur="handerBlur" @focus="handerFocus" v-bind:value="txt" v-on:input="handerInput"> </p> </template> <script> export default { name: 'z-input', data () { return { txt: '', txt2: '', showBig: false, } }, methods: { handerInput (evt) { let val = evt.target.value.substr(0, 13) // 限制长度13位 this.txt = val evt.target.value = val if (this.txt.length > 0) { this.showBig = true } this.txt2 = this.getStr(this.txt, 4) }, handerBlur () { this.showBig = false }, handerFocus (evt) { if (evt.target.value.length > 0) { this.showBig = true } }, getStr (str, len) { let lenth = str.length let len1 = len - 1 let newStr = '' for (var i = 0; i < lenth; i++) { if (i % len === len1 && i > 0) { newStr += str.charAt(i) + '_' } else { newStr += str.charAt(i) } } if (newStr.length % (len + 1) === 0) { // 解决最后一位为补充项问题 newStr = newStr.substr(0, newStr.length - 1) } return newStr }, } } </script>
Restrict only numbers to be entered
The first thing that comes to mind is to filter out non-numbers when keyup
But The fact is that keydown
->handerInput
->keyup
is executed first, then it will be processed at keydown
, but after keydown modifies evt.target.value
, the
evt.target.value obtained in
handerInput is still unprocessed, so it is said that in
keydownProcessing does not work
Must be processed at
handerInput
<input type="text" @blur="handerBlur" @focus="handerFocus" @keyup="handerKeyup" @keydown="handerKeydown" v-bind:value="txt" v-on:input="handerInput"> <script> handerKeydown (evt) { console.log('handerKeydown') evt.target.value = evt.target.value.replace(/[^\d]/g, '') // 这里修改不起作用 }, handerKeyup (evt) { console.log('keyup') evt.target.value = evt.target.value.replace(/[^\d]/g, '') // 这里执行顺序靠后 修改无用 }, handerInput (evt) { let val = evt.target.value.substr(0, 13).replace(/[^\d]/g, '') console.log('handerInput__val', val) //这里拿到的val是纯数字 evt.target.value = val this.txt = val if (this.txt.length > 0) { this.showBig = true } this.txt2 = this.getStr(this.txt, 4) }, </script>
This is an interview for a certain alliance Question Isn’t it all about having a clear idea and optimizing it while writing
At that time, I mentioned the idea of using calculated properties to calculate the value of the preview box based on the content of the input box
Dealing with focus Just use the blur event
You have to write it by hand, I can’t write it by hand, and then the interviewer thinks I won’t be speechless
<template lang="html"> <p class="zInput"> <p class="big-show" v-show="showBig">{{txt2}}</p> <input type="text" @blur="handerBlur" @focus="handerFocus" v-model="txt"> </p> </template> <script> export default { name: 'z-input', data () { return { txt: '', showBig: false, } }, computed: { txt2: function () { if (this.txt.length > 0) { this.showBig = true } return this.getStr(this.txt, 4) } }, methods: { handerBlur () { this.showBig = false }, handerFocus (evt) { if (evt.target.value.length > 0) { this.showBig = true } }, getStr (str, len) { let lenth = str.length let len1 = len - 1 let newStr = '' for (var i = 0; i < lenth; i++) { if (i % len === len1 && i > 0) { newStr += str.charAt(i) + '_' } else { newStr += str.charAt(i) } } if (newStr.length % (len + 1) === 0) { // 解决最后一位为补充项问题 newStr = newStr.substr(0, newStr.length - 1) } return newStr }, } } </script> <style lang="less"> .zInput{ position: relative; .big-show { position: absolute; top: -40px; font-size: 36px; line-height: 40px; background-color: red; } } .zInput{ top:50px; } </style>
v-model is actually a syntactic sugar
written separately as
v-bind:value and
v-on:input
<template lang="html"> <p class="zInput"> <p class="big-show" v-show="showBig">{{txt2}}</p> <input type="text" @blur="handerBlur" @focus="handerFocus" v-bind:value="txt" v-on:input="handerInput"> </p> </template> <script> export default { name: 'z-input', data () { return { txt: '', txt2: '', showBig: false, } }, methods: { handerInput (evt) { let val = evt.target.value.substr(0, 13) // 限制长度13位 this.txt = val evt.target.value = val if (this.txt.length > 0) { this.showBig = true } this.txt2 = this.getStr(this.txt, 4) }, handerBlur () { this.showBig = false }, handerFocus (evt) { if (evt.target.value.length > 0) { this.showBig = true } }, getStr (str, len) { let lenth = str.length let len1 = len - 1 let newStr = '' for (var i = 0; i < lenth; i++) { if (i % len === len1 && i > 0) { newStr += str.charAt(i) + '_' } else { newStr += str.charAt(i) } } if (newStr.length % (len + 1) === 0) { // 解决最后一位为补充项问题 newStr = newStr.substr(0, newStr.length - 1) } return newStr }, } } </script>
The first thing that comes to mind is to filter out non-digits during
keyupBut the fact is that
keydown->
is executed first handerInput->
keyup
keydown, but after keydown changes
evt.target.value
The
evt.target.value obtained in
handerInput is still unprocessed, so processing in
keydown does not work
must be done in
handerInputTime processing
<input type="text" @blur="handerBlur" @focus="handerFocus" @keyup="handerKeyup" @keydown="handerKeydown" v-bind:value="txt" v-on:input="handerInput"> <script> handerKeydown (evt) { console.log('handerKeydown') evt.target.value = evt.target.value.replace(/[^\d]/g, '') // 这里修改不起作用 }, handerKeyup (evt) { console.log('keyup') evt.target.value = evt.target.value.replace(/[^\d]/g, '') // 这里执行顺序靠后 修改无用 }, handerInput (evt) { let val = evt.target.value.substr(0, 13).replace(/[^\d]/g, '') console.log('handerInput__val', val) //这里拿到的val是纯数字 evt.target.value = val this.txt = val if (this.txt.length > 0) { this.showBig = true } this.txt2 = this.getStr(this.txt, 4) }, </script>
Use regular rules to verify the bank card number entered by the user (with code)
js, jq How to verify bank card account code sharing
Mobile phone number, email address, ID card, bank card regular verification example
The above is the detailed content of Simply realize the synchronization display function of mobile phone number and bank card - My column Sifu. For more information, please follow other related articles on the PHP Chinese website!