简体   繁体   English

如何在jQuery中将输入限制为仅句点,数字和分号?

[英]How can I restrict input to only periods, numbers and semicolons in jQuery?

I want to restrict what characters a user can type in an input text field such that they can only type in numbers and periods and colons. 我想限制用户可以在输入文本字段中键入哪些字符,以便他们只能键入数字,句点和冒号。 So if they type anything else, it won't show up or register. 因此,如果他们键入其他任何内容,它将不会显示或注册。 This is for IP addresses for example: 10.200.66.106:8888 例如,用于IP地址:10.200.66.106:8888

How can I achieve this in jQuery? 如何在jQuery中实现呢?

I've written a little example in jsFiddle: http://jsfiddle.net/6xMxs/ 我在jsFiddle中写了一个小例子: http : //jsfiddle.net/6xMxs/

HTML: HTML:

<div id="outer">
    <div id="inner">
            <p>IP Address:</p>
        <p class="edit">2323</p>
    </div>
<div>

jQuery: jQuery的:

 $(document).ready(function() {
 $('.edit').editable('ipaddress/update',{ 
     type      : 'textarea',
     cancel    : 'Cancel',
     submit    : 'OK',        
     tooltip   : 'Click to edit...'
 });

}); });

You can use jquery's alphanumeric plugin to do that. 您可以使用jquery的字母数字插件来执行此操作。 Add some class to your element. 在您的元素中添加一些类。

$(".myclass").numeric({allow:"."});

You could remove not-allowed characters: 您可以删除不允许的字符:

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



$(document).ready(function() {

     $('#ip').keyup(function() { 
        var el = $(this),
            val = el.val();

         el.val(val.replace(/[^\d\.]/gi, ""));
     }).blur(function() {
         $(this).keyup();
     });
 });

Working fiddle 工作提琴

But there are better ways, you can for example completely "block" the entering of unwanted characters. 但是有更好的方法,例如,您可以完全“阻止”不需要的字符的输入。 And remember, you can easily avoid these restrictions when no server-side validation is being done. 请记住,当不进行服务器端验证时,您可以轻松避免这些限制。

Also, you might want to checkout the html5 attribute pattern which, as the context suggests, only works for html5 browsers. 另外,您可能想签出html5属性模式 ,如上下文所示,该模式仅适用于html5浏览器。

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

相关问题 如何将文本输入仅限制为数字,但允许使用10键输入数字? - How do I restrict a text input to numbers only but allow numbers from a 10 key? Javascript / Jquery将输入字段限制为一个小数和数字 - Javascript/Jquery to restrict input field to one decimal and numbers only 对话框将输入限制为仅数字 - Dialogbox restrict input to numbers only 如何限制“正数”仅作为文本框的输入(仅允许“-99”)? - How to restrict “positive numbers” only as input to textbox (allow “-99” ONLY)? 我可以将 contenteditable td 限制为只允许数字吗? - Can I restrict a contenteditable td to only allow numbers? Oracle Apex:如何将表单中的数字字段限制为仅允许数字? - Oracle Apex: How can I restrict a numeric field in a form to allow numbers only? 将输入限制为仅特定字符和6位数字 - Restrict a input to only specific character and 6 digit of numbers 仅在jQuery中输入数字 - Input numbers only in jquery 如何使用jQuery限制输入到输入文本元素中的文本? - How can I restrict the text entered into an input text element using jQuery? 如何使用CSS(或jQuery,如有必要)限制输入HTML输入的字符数? - How can I restrict the number of characters entered into an HTML Input with CSS (or jQuery, if necessary)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM