vue 表单禁用数字以外的输入
XML/HTML Code复制内容到剪贴板
- <label>
- <h3>价格</h3>
- <span><input type="number" v-model="publish_price" pattern="[0-9]*"><i>元</i></span>
- </label>
只弹出数字键盘
2、保留一位小数:
XML/HTML Code复制内容到剪贴板
- <span><input type="number" v-model="publish_price" v-on:input="inputPrice" /><i>元</i></span>
- <script>
- // 价格正则
- inputPrice(){
- var reg = /^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1})?$/;
- console.log(this.publish_price);
- if(!reg.test(this.publish_price)){
- this.$toast("最多保留一位小数");
- this.publish_price = parseFloat(this.publish_price).toFixed(1);
- return false;
- }
- },
- </script>
保留两位小数的正则:
JavaScript Code复制内容到剪贴板
- // 价格正则
- inputPrice(){
- var reg = /^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/;
- console.log(this.publish_price);
- if(!reg.test(this.publish_price)){
- this.$toast("最多保留一位小数");
- this.publish_price = parseFloat(this.publish_price).toFixed(2);
- return false;
- }
- },
限制表单长度:
XML/HTML Code复制内容到剪贴板
- <input type="text" v-model="publish_title" v-on:input="limitTitle">
上面的js部分的截取有问题,这样也可以:
XML/HTML Code复制内容到剪贴板
- <input type="text" v-model="publish_title" maxlength="31">
JavaScript Code复制内容到剪贴板
- data: function() {
- return {
- publish_title: "",
- }
- },
- methods: {
- limitTitle() {
- //限制标题长度
- let title = this.publish_title;
- let length = title.replace(/[^\x00-\xff]/g, "**").length;//得到输入的字节数
- let limit = 31;
- if(length >= limit) {
- let instance = this.$toast('标题已超出长度');
- setTimeout(() => {
- instance.close();
- }, 1000);
- this.publish_title = title.substr(0,limit);
- return false;
- }
- },