问题
I'm using a Jquery numeric plugin that only allows numeric values to be typed in an input
$("#tbQuan").numeric();
In addition to what this plugin is doing I need to disable typyng '0' (zero) at the first character position.
Any help would be appreciated.
回答1:
Try this.
$('input').keypress(function(e){
if (this.value.length == 0 && e.which == 48 ){
return false;
}
});
Demo
回答2:
Something like this should get you started:
$("#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
});
回答3:
For the keyUp event
$('.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
- make sure to add both 48 and 96 events to support number pad 0 and keyboard 0.
- adding this way, user get to know that leading zeros are not allowed
回答4:
I'd start by trying something like this:
$('input').keyup(function(){
if ( $(this).val().length === 1 && $(this).val() === 0 ){
alert('No leading zeroes!');
}
);
回答5:
if textbox in masked then:::
$('#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);
});
OR *******************************
$("#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");
}
});
First Sample DEMO :
Second Sample DEMO :
来源:https://stackoverflow.com/questions/9106603/disable-typing-0-zero-at-first-character-position