简体   繁体   English

如何在按键事件中仅允许特定打印范围内的字符?

[英]How to allow only characters on key press event for specific print range?

<input type="text" name="RangeText" id="rangeNum" onkeypress = "printValidation(event)" />

I am trying to allow only characters on key press event for specific pages range in the above print selection text box. 我试图在上述打印选择文本框中仅允许特定页面范围的按键事件上的字符。 I can do this easily with regex but want user to type only valid sccenarios! 我可以使用正则表达式轻松完成此操作,但希望用户仅键入有效脚本!

What I want to achive is as shown in below examples: 我要达到的目标如以下示例所示:

1 allow 1允许

1-2 allow 1-2允许

-2 not allow -2不允许

1, 2-3, 4, 5-7 allow 1,2-3,4,5-7允许

1 2, 3 not allow 1,2,3不允许

1-2-2 not allow 1-2-2不允许

This is how my current funtion looks like, the tricky part seems is not allowing negative number to begin with but allow the hyphen in the range! 这就是我当前的功能,棘手的部分似乎是不允许以负数开头,但允许连字符在范围内!

function printValidation(evt) {
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (!evt.shiftKey && !evt.altKey && !evt.ctrlKey && charCode >= 48 && charCode <= 57 || charCode == 188 || charCode == 109) {
        alert("character accepted");
        return true;
    }else {
            alert("illegal character detected");
            return false;
        }
 }

Is there a possibility to achieve this without going in regex way? 是否有可能不用正则表达式来实现这一目标?

I suggest you allow the user to type only valid characters as you are doing (including hyphen, comma and space) and run a regex at the end (when you submit or lose focus of the field). 建议您在执行操作时仅允许用户键入有效字符(包括连字符,逗号和空格),并在末尾运行正则表达式(当您提交或失去该字段的焦点时)。 Or you could fill your function with a lot of if to test all the possibilities, ex. 或者你可以用很大的填补你的函数if测试一切准备,恩。 allow hyphen only after another character that's not hyphen, comma or space, etc. You can read the content of the field with evt.currentTarget.value . 只能在另一个非连字符,逗号或空格等字符之后使用连字符。您可以使用evt.currentTarget.value读取字段的内容。 Hope that helps :) 希望有帮助:)

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

相关问题 如何在按键或按键事件中使用javascript在文本字段中仅允许18个带小数点的2个小数点 - how to allow only 18 nos with 2 decimal point in text field using javascript on key up or key press event jQuery仅在按键时允许字符之间留有空格 - Jquery Allow Empty space between Characters only on Key press 只允许输入中的特定字符 - Only allow specific characters in input 如何允许用户只输入密码的特定字符并在 HTML 中验证它? - how to allow user to enter only specific characters of password and validate it in HTML? 如何模拟按键事件? - How to simulate key press event? Javascript正则表达式只允许特定字符 - Javascript regex allow only specific characters 在keypressup事件上按一个键时,textarea打印两个字符 - textarea print a two char when press a single key on keypressup event 反应如何在按键时触发特定的滚动事件(div 内的箭头向上或箭头向下) - React how to trigger a specific scroll event(arrowUp or arrowDown inside a div) on a key press 仅允许将活动保存到特定月份的全日历中 - only allow to save an event to a specific month in fullcalendar 我想检查仅允许空格、AZ、az、0-9 的文本字段上的按键或按键事件中特殊字符的验证 - I want to check validation of special character on key-press or key-up event on a text field which allow space,A-Z,a-z,0-9 only
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM