判断input只能输入数字/字母/文字...

使用正则表达式

以下方式是使用了uniapp开发,vue2版本

 input标签:

<input type="number" placeholder="备注" v-model="price" @input="fpNumInput">

js部分:

//判断指定的input框,限制输入的格式
			fpNumInput(e) {
				const o = e.target;
				const inputRule = /[^\d]/g; //只能输入数字
				this.$nextTick(function() {
					this.price = o.value.replace(inputRule, '');
				})
				if (this.price != o.value.replace(inputRule, '')) {
					uni.showToast({
						title: '请输入数字',
						icon: 'none',
					})
				}
			},

结果:

格式:

1.只能输入数字

const inputRule = /[^\d]/g		

2.只能输入字母

const inputRule = /[^a-zA-Z]/g		

3.只能输入小写格式字母

const inputRule =/[^a-z]/g

4.只能输入数字和字母

扫描二维码关注公众号,回复: 15226841 查看本文章
const inputRule =/[\W]/g

5.只能输入数字和小数点

const inputRule =/[^\d.]/g

6.只能输入大写字母

const inputRule =/[^A-Z]/g

7.只能输入数字和字母和下划线

const inputRule =/[^A-Z]/g

8.只能输入中文

const inputRule =/[^\u4E00-\u9FA5]/g

猜你喜欢

转载自blog.csdn.net/CQXXTXX/article/details/130539624