[英]Allow only numbers and underscore(_) using jQuery
我需要像這樣的有效輸入122_123
。 如果輸入的ID錯誤,則需要顯示一條錯誤消息。 這必須使用jQuery完成。 我有下面的代碼,由於某種原因,它似乎不起作用。
$('[id*=search]').unbind().click(function() {
var numPattern = /^[0-9_]+$/;
//check if the copy pasted value of RequestId has alphabets.
if (!(/^[_0-9]*$/i).test($('[id*=reqId]').val()){
alert('Invalid');
$('[id*=reqId]').val('');
return false;
}
displaySpinner();
return true;
});
解決此問題的最佳方法是什么?
我認為您實際上正在檢查.test()
對立面。 您應該刪除!
從條件不簽:
if ((/^[_0-9]*$/i).test($('[id*=reqId]').val()){
檢查一下,這可能對您有幫助:-
$('document').ready(function() { $('#input_id').bind('keyup', function(e) { console.log($('#input_id').val()); if ((/[_0-9]$/i).test($('#input_id').val())) { $('p').html("true"); } else { $('p').html("false"); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <input id="input_id" type="text" /> <p></p>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.