How can I make a numeric input with min length 4, max 5 and not 0;
like you can input 1234 but 5th digit shouldn't be 0; So no 12340 also no more than 4999?
Answer by https://stackoverflow.com/users/665261/billy-moon :
replace(/[^0-9]/,'') -> makes numeric; replace(/^([0-9]{4})[^1-9]+/,'$1') -> makes 5th digit not 0 replace(/^[^0-4]/,'') ->makes first digit 0-4Code:
$('input').bind('keyup change',function(){
$(this).attr({开发者_StackOverflow中文版 value: $(this).attr('value').replace(/[^0-9]/,'').replace(/^([0-9]{4})[^1-9]+/,'$1').replace(/^[^0-4]/,'') })
})
I would add an keyup event that validates the field, and replaces bad input something like this
$('input').bind('keyup change',function(){
$(this).attr({ value: $(this).attr('value').replace(/[^0-9]/,'').replace(/^([0-9]{4})[^1-9]+/,'$1') })
})
you can't set a "min-length", but you can validate the input whenever you want (on change, form submit, etc):
if(!input.value.test(/^\d{4}[1-9]?$/)){
// fail
}
精彩评论