简体   繁体   English

防止输入数字和特殊字符进行输入

[英]Prevent entering numeric and Special Characters for input

I have written a method that captures the entered key through event.我写了一个方法,通过事件捕获输入的键。

NameValidation(e){

    if(!e.key.match(/^[a-zA-Z]*$/))
    {
        e.key = '';
        console.log(e.key);
    }
},

What this does is basically checks if a user has entered a special character or number and if so replaces the special character/number to an empty string.这样做基本上是检查用户是否输入了特殊字符或数字,如果是,则将特殊字符/数字替换为空字符串。

The issue starts with e.key = '';问题开始于e.key = ''; . .

As it does not replace the captured key.因为它不会替换捕获的密钥。 What am I missing here ?我在这里错过了什么?

I realized i was using Keypress event which was delayed in processing the trigger我意识到我正在使用 Keypress 事件,该事件在处理触发器时被延迟

So instead i used keydown and then used preventDefault to manipulate default event behaviour因此,我使用 keydown 然后使用 preventDefault 来操作默认事件行为

This article Helped me How can I prevent numeric inputs using VueJS这篇文章帮助我如何使用 VueJS 防止数字输入

Here my Answer这是我的答案

JS JS

      NameValidation(e){

            if(!e.key.match(/^[a-zA-Z]*$/))
            {
                e.preventDefault();
            }
           
        },

Html html

<input v-model.trim="firstname_txt" v-on:keydown="NameValidation($event)">
                                                            
     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM