简体   繁体   English

如何只输入有限的字母 <input type=“text” /> 用jQuery?

[英]How to make only limited letters can be typed into <input type=“text” /> with jQuery?

例如,仅允许输入数字:

<input type="text" id="target" />

Use the keypress() event. 使用keypress()事件。 This example prevents characters that aren't digits (0 = 48, 9 = 57). 本示例防止使用非数字字符(0 = 48、9 = 57)。

$(function() {
  $("#target").keypress(function(evt) {
    if (evt.which < 48 || evt.which > 57) {
      return false;
    }
  });
});

See this list of Javascript key codes . 请参阅此Javascript关键代码列表

var value = $('#target').val();

function isNumber ( n ) { return !isNaN( n ) }

isNumber(value) // if its '3' then true
isNumber(value) // if 3 then true
isNumber(value) // if '' then false

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

相关问题 如何验证仅来自 Jquery 或 HTML5 的输入文本 X 和 Y 字母 - How to validate in the input text X and Y letters only from Jquery or HTML5 仅当在文本字段中键入正确的单词时,才能使链接有效? - How can I make a link work, only when a correct word is typed into a text field? 如何创造价值 <input type=“text”> 用jQuery选中? - How to make value of <input type=“text”> selected with jQuery? 如果将文本输入到表单输入中,如何显示div - How to make a div appear if text gets typed into a form input 如何使输入中键入的文本显示在单独的div中 - How to make the text that are typed inside the input to appear into a seperate div 如何显示以密码输入类型键入的文本,否则隐藏密码 - how to show text typed in a password input type otherwise hide the password 如何检查是否有人在输入文字上添加了4个字母或更多? - How to check if someone has added 4 letters or more on input type text? jQuery-如何检查输入是否仅包含西里尔字母 - jQuery - How to check if input contains only cyrillic letters 突出显示输入框中键入的字母 - Highlight letters typed in the input box 检测输入中键入的单词和字母,并对具有相同字母的其他元素(div中的文本)进行排序 - Detect words and letters typed in input and sort other elements (text in div) with same letters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM