[英]protect the text box to not take that blank value after clearing the previous value
我想保護文本框不接受數字以外的任何值。 我用以下代碼實現了
function allowOnlyNumber(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
問題是:當我在文本框中輸入數字並通過單擊鏈接到它的UPDATE按鈕進行保存時,它會得到預期的保存。 但是第二次,當我從該文本框中選擇數字並通過鍵盤上的Delete鍵或Backspace鍵清除它,然后單擊鏈接到它的UPDATE按鈕時,空白值將被更新。
如何保護文本框不采用該空白值? 它應該保留以前的有效值。
在OnLoad函數上使用此代碼段
var preVal = $("#txt").val();
$("#txt").change(function()
{
if($(this).val().trim() == '')
{
$(this).val(preVal);
}
preVal = $("#txt").val();
}
);
工作的小提琴在這里
您可以使用$.trim()
jQuery方法來防止空白值被提交。
像這樣嘗試:
var elem = $('yourelemId').val(); // get the specific value
if($.trim(elem) === ""){ // use trim to remove any whitespace
alert('Your alert msg here');
e.preventDefault();
}
這樣做。短代碼根本不接受數字。
$("#txt").keyup(function() {
this.value = this.value.replace(/[^a-zA-Z\.]/g,'');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.