简体   繁体   English

如何使我的输入仅限于最多 10 个数字并且不允许任何字符?

[英]How can I make my input to be only limited to a maximum of 10 numbers and don't allow any characters?

I want to be able to put a maximum length for the phone number entered and don't allow any characters too.我希望能够为输入的电话号码设置最大长度,并且也不允许任何字符。 However, with the format below it does limit the person to enter 10 characters but it also allows any other character to be added too.但是,使用下面的格式,它确实限制了一个人输入 10 个字符,但它也允许添加任何其他字符。

Now if I change type to type="number" maxLength will not work and also characters such as [minus(-) or plus(+)] will still go through.现在,如果我将type更改为type="number" maxLength 将不起作用,并且 [minus(-) 或 plus(+)] 等字符仍将通过 go。

How can I make my input to be only limited to a maximum of 10 numbers and don't allow any characters?如何使我的输入仅限于最多 10 个数字并且不允许任何字符?

<input
  placeholder="Telephone Number"
  className="input_fields telephone_no"
  type="tel"
  maxLength={"10"}
  name="phoneNumber"
  id="teacher_telephone_no"
  value={this.state.user.phoneNumber}
  onChange={e =>
    this.setState({
      user: {
        ...this.state.user,
        [e.target.name]: e.target.value
      }
    })
  }
/>

You could use regular expression and keyup event to prevent symbol from typing.您可以使用正则表达式和keyup事件来防止输入符号。

 let input = document.querySelector('input') input.onkeyup = function() { input.value = input.value.replace(/[^\d]/, "") }
 <input placeholder="Telephone Number" className="input_fields telephone_no" type="tel" maxLength={ "10"} name="phoneNumber" id="teacher_telephone_no" />

You can use regex to test your input before setting the state, as:在设置 state 之前,您可以使用regex来测试您的输入,如下所示:

     <input
        placeholder="Telephone Number"
        className="input_fields telephone_no"
        type="tel"
        maxLength={"10"}
        name="phoneNumber"
        id="teacher_telephone_no"
        value={user}
        onChange={(e) => {
          if (/^[0-9]{1,10}$/.test(e.target.value)) {
             this.setState({
               user: {
               ...this.state.user,
               [e.target.name]: e.target.value
               }
            })
          }
        }}
      />

暂无
暂无

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

相关问题 如何在我的输入中只允许数字和特殊字符? - How do I allow only numbers and special characters in my input? 如何使用 Jquery 使我的文本框仅包含数字和(逗号)以及空格和 + 字符? - How Can I make it possible that my Text box will take only numbers and ,(coma) and spaace and + characters using Jquery? 只允许英文字符和数字进行文本输入 - only allow English characters and numbers for text input 如何只允许数字作为Javascript中的输入,为什么我的代码不起作用? - How to allow only numbers as input in Javascript , why doesn't my code work? RegEx允许输入标签的任何字符最多为255个 - RegEx allow any characters for a maximum of 255 for an input tag 进行输入时仅允许使用姓名,而不能使用数字 - Make a input only allow names and not numbers 我怎样才能使这个按钮文本允许您输入图片但在上传图片时消失并在您不选择照片时返回? - How can I make this text that is button allow you to input pictures but disappear when picture is uploaded and comes back if you don't chose a photo? 我需要让用户输入任何数字和字符,并且只能在字段中输入“-”或“ \\”。 我该如何使用正则表达式呢? - I need to let the user input any numbers and characters and only the '-' or the '\' in a field. How do I do that with regular expression? 如何允许在陷阱中仅输入数字的“ +”和“()”? - How do I allow the “+” and “()” in my trappings for numbers only to be entered? 如果我的计算机的 Windows 10 不允许,我该如何安装 npm? - How can I install npm if my computer's windows 10 doesn't allow it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM