简体   繁体   English

如何限制用户在反应 class 组件中的文本输入字段中输入数字或特殊字符

[英]How to restrict user from entering numbers or special characters in to text input field in react class components

im trying to restric user from entering numbers using regex in to my text type input.我试图限制用户在我的文本类型输入中使用正则表达式输入数字。 how can i achieve it,我怎样才能实现它,

  nameChangeHandler=(e)=>{
    const userInput={...this.state.userInput};
    const reg=/[0-9]/;
    if(reg.test(e.currentTarget.value))
    e.preventDefault();
    userInput[e.currentTarget.name]=e.currentTarget.value
    this.setState({
      userInput
    })
  }
 <input id="nameInput"  type="text" name="username" value={username} onChange={this.nameChangeHandler} />

This is what i have tried doing这就是我尝试做的

The regex pattern to "not allow" for numbers (= check whether numbers are not in the string): /^([^0-9]*)$/正则表达式模式“不允许”数字(= 检查数字是否不在字符串中): /^([^0-9]*)$/

Example regex to additionally check for special characters ($ and %): /^([^0-9$%]*)$/额外检查特殊字符($ 和 %)的示例正则表达式: /^([^0-9$%]*)$/

Assuming your current handler works, this should prevent users from typing a number and % and $:假设您当前的处理程序有效,这应该会阻止用户输入数字和 % 和 $:

nameChangeHandler=(e)=>{
    const reg=/^([^0-9$%]*)$/;
    if (reg.test(e.currentTarget.value)) {
      this.setState({ ...this.state.userInput, username: e.currentTarget.value })
    }
  }

暂无
暂无

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

相关问题 如何限制用户在类型为文本的输入字段中输入数字? - How to restrict user from entering number in to an input field with type as text? 如何限制输入框接受Angular中的特殊字符? - How to restrict input field from accepting special characters in Angular? 如何使用 Angular 2 / Typescript 限制输入字段中的特殊字符 - How to restrict special characters in the input field using Angular 2 / Typescript 如何防止用户在长度为0时在文本框中输入特殊字符? - How to prevent user from entering special characters in text box when length is 0? 如何限制mat-form-field(matInput)用户仅输入字符而特殊字符而不输入数字? - How to restrict mat-form-field (matInput) user enter only characters and special characters not number? Javascript 正则表达式限制用户在文本字段中输入相同的连续数字 - Javascript Regular Expression to restrict user from Entering same consecutive digits in a text field 如何限制用户在Chrome扩展程序的选项中输入浮点数 - How to restrict user from entering floating point numbers in Chrome extension's options 如何防止用户在输入字段中输入字符“`”? - How to prevent the user from entering the character "`" in the Input field? 如何限制无效值输入具有正则表达式模式的输入字段 - How to restrict invalid values from entering in an input field having a regex pattern 限制特殊字符进入输入框 - restrict special characters into input box
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM