UniApp Input框限制输入数字,阻止非数字字符 - 代码示例
<p><template>/n <view>/n <input @input='handleInput' placeholder='只能填写数字' />/n </view>/n</template>/n/n<script>/nexport default {/n methods: {/n handleInput(e) {/n // 使用正则表达式判断输入的内容是否为数字/n const reg = /^/d+$/;/n const value = e.target.value;/n/n if (!reg.test(value)) {/n uni.showToast({/n title: '只能填写数字',/n icon: 'none',/n duration: 2000/n });/n/n // 清空输入框的内容/n e.target.value = '';/n }/n }/n }/n};/n</script>/n/n在上述代码中,通过在input标签上绑定了input事件,并在事件回调handleInput中判断输入的内容是否为数字。如果不是数字,则通过uni.showToast方法显示提示信息,并将输入框的内容清空。/n/n需要注意的是,该示例只限制了输入内容为数字,如果还需要限制输入长度、特殊字符等,可以根据需求进行相应的修改。</p>
原文地址: https://www.cveoy.top/t/topic/br9O 著作权归作者所有。请勿转载和采集!