禁用在第一个字符位置键入0(零)
我正在使用一个Jquery数字插件,它只允许在输入中输入数值
$("#tbQuan").numeric();
除了这个插件正在做什么之外,我需要在第一个字符位置禁用typyng’0’(零)。
任何帮助,将不胜感激。
试试这个。
$('input').keypress(function(e){ if (this.value.length == 0 && e.which == 48 ){ return false; } });
演示
这样的事情应该让你开始:
$("#tbQuan").numeric().keyup(function (e) { var val = $(this).val(); while (val.substring(0, 1) === '0') { //First character is a '0'. val = val.substring(1); //Trim the leading '0' } $(this).val(val); //update input with new value });
对于keyUp事件
$('.numeric').keyup(function(event) { var currentVal = $(this).val(); if (currentVal.length == 1 && (event.which == 48 || event.which == 96)) { currentVal = currentVal.slice(0, -1); } $(this).val(currentVal);
});
DEMO
- 确保添加48和96事件以支持数字键盘0和键盘0。
- 以这种方式添加,用户可以知道不允许前导零
我首先尝试这样的事情:
$('input').keyup(function(){ if ( $(this).val().length === 1 && $(this).val() === 0 ){ alert('No leading zeroes!'); } );
如果掩盖的文本框然后:::
$('#numeric').keyup(function(event) { var currentVal = $(this).val(); if (currentVal.substring(0, 1) === '0' && (event.which == 48 || event.which == 96)) { currentVal=currentVal.substring(1); } $(this).val(currentVal); });
要么 *******************************
$("#numeric").keyup("input propertychange paste", function (e) { var val = $(this).val() var reg = /^0/gi; if (val.match(reg)) { $(this).val(val.replace(reg, "")); alert("Please phone number first character bla blaa not 0!"); $(this).mask("999 999-9999"); }
});
第一个样本演示:
第二个样本DEMO: