Sangram和miklshake的回答帮助了我。以下是综合解决方案。
$(document).on('keyup','.major_rev', function(event){
var input = event.currentTarget.value;
if(input.search(/^0/) != -1){
alert("you have started with a 0");
}
});
$(document).ready(function() {
//For numeric
$(".major_rev").keydown(function(event) {
// Allow only backspace and delete
if ( event.keyCode == 46 || event.keyCode == 8) {
// let it happen, don't do anything
}
else {
// Ensure that it is a number and stop the keypress
if ((event.keyCode !==9) && (event.keyCode < 48 || event.keyCode > 57 )) {
event.preventDefault();
}
else{
if($.trim($(this).val()) =='')
{
if(event.keyCode == 48){
event.preventDefault();
}
}
}
}
});
$(".minor_rev").keydown(function(event) {
// Allow only backspace and delete
if ( event.keyCode == 46 || event.keyCode == 8 ) {
// let it happen, don't do anything
if($.trim($(this).val()).length==0)
{
if(event.keyCode==48){
event.preventDefault();
}
}
}
else {
// Ensure that it is a number and stop the keypress
if (event.keyCode < 48 || event.keyCode > 57 ) {
event.preventDefault();
}
else{
if($.trim($(this).val()) =='')
{
if(event.keyCode == 48){
event.preventDefault();
}
}
}
}
});
//For MaxLength
$(".major_rev").prop("maxlength","4");
$(".minor_rev").prop("maxlength","4");
});
jsFiddle