我有一个检查长度和输入是否为空的函数,我想知道是否可以同时检查输入是否是数字。我寻找这样的东西:如果(字段值。长度> 0 &&isNaN)
function checkNotEmpty(field,span){
if (field.value.length > 0){
document.getElementById(span).className='ok'
document.getElementById(span).innerHTML='its ok';
}
else {
document.getElementById(span).className='notok'
document.getElementById(span).innerHTML='its not ok';
}
}
除非我错过了什么,否则看起来你已经有了答案。
if (field.value.length > 0 && isNaN(field.value)) {
//...value is at least 1 character long and field IS not a number
}
你在找这个吗?
function checkNotEmpty(field, span) {
let value = parseInt(field.value);
if (isNaN(value) || value < 0) {
// Either it's not a number or it's negative.
// React appropriately.
} else {
// We're good.
}
}