简体   繁体   English

如何验证仅来自 Jquery 或 HTML5 的输入文本 X 和 Y 字母

[英]How to validate in the input text X and Y letters only from Jquery or HTML5

I try to validate in the text box X and Y letters only for the NIC field.我尝试在文本框中仅验证 NIC 字段的 X 和 Y 字母。 But it's not working.但它不起作用。 I need to block other letters without X and Y.我需要阻止没有 X 和 Y 的其他字母。

Example like this - 881161537V , 881161537X像这样的例子 - 881161537V , 881161537X

This is Sri Lankan NIC format.这是斯里兰卡的 NIC 格式。

I appreciate your help.我感谢您的帮助。 Many thanks!非常感谢!

HTML HTML

<input type="text" name="number">

JS JS

$("input[name='number']").on('keydown', function(e) {
    var key = e.keyCode ? e.keyCode : e.which;
    //alert(key);
    if (!([8, 9, 13, 27, 46, 110, 190,17, 89, 88].indexOf(key) !== -1 ||
            (key == 65 && (e.ctrlKey || e.metaKey)) ||
            (key >= 35 && key <= 40) ||
            (key >= 48 && key <= 57 && !(e.shiftKey || e.altKey)) ||
            (key >= 96 && key <= 105)
        )) e.preventDefault();

});

Working Fiddle工作小提琴

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM