簡體   English   中英

僅允許 jquery 表單輸入中的數字和多個點

[英]Allow only numbers and multiple dots in jquery form input

我想知道在表單輸入中使用 jquery 時只允許數字和多個點的方法。

場景是表單輸入包含一個版本名稱,版本名稱可以具有像 6.0.2345 這樣的值,就我的研究而言,我只能通過以下代碼獲得數字:

$('.number').keydown(function(event) {
    if(event.which < 46
    || event.which > 59) {
        event.preventDefault();
    } // prevent if not number/do
});

但這只允許數字,不允許我使用退格或刪除。 我可以使用此方法的任何解決方法,以便在輸入文件中只允許多個點和數字。

終於得出了這個有效的答案。

$(".submit").on('click',function(e){
        e.preventDefault();
        var test_val = $(".test").val();
        var test_regex = /^(\*|\d+(\.\d+){0,3}(\.\*)?)$/i;
        if(!test_regex.test(test_val)){
            alert("This is not a valid entry");
        }
});
<input onKeyPress={(event) => { if (!/[0-9.]/.test(event.key)) { event.preventDefault(); } }}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM