Thank you in advance. I wrote an Input with type = number. Due to business needs, only numbers can be entered in this Input, and the plus and minus signs cannot be entered.
Originally I wanted to judge the value in the input and manually delete the plus signs, minus signs and redundant dots, but I found that once the input value is illegal, the value of the input has been converted into an empty string ''. In this way I cannot get the value in the input.
I’ve been thinking about it for a long time but don’t know the solution. Please help me, thank you~
By the way, I wrote the page in vue~~~
Use regular expression to match '^[-]?[0-9]*.?[0-9]+(eE?[0-9]+)?$'
You can use this method to monitor the changes in the input value. If you find that the verification fails, you will be prompted.
Post your js and take a look
onkeyup="this.value=this.value.replace(/D/g,'')" onafterpaste="this.value=this.value.replace(/D/g,'')"
type=number should not be able to directly restrict users from entering positive and negative signs, e, and multiple decimal points.
It still needs to be judged by the value of the input.
As for the problem that the input value is illegal and the input value is converted into an empty string,
You can change the input type to text, and then use regular expressions and replace to remove non-digits and multiple decimal points.
Then you can use v-model to bind a value, and then monitor the value through watch. But when you find an illegal value, just delete the illegal string
<input type="tel" />