$('#empcontact').blur(function(){
var stri = $('#empcontact').val();//the input element
var numbers = "0123456789";
var flag = false;
for(var x=0;x<stri.length;x++){
var ch = stri.charAt(x);
var n = numbers.indexOf(ch);
if(n === -1){//why does it always resolve to true
flag = true;
break;
}
else{
}
}
if(flag){
alert("Not a number");
$('#empcontact').val(" ");
$('#empcontact').focus();
}
});
Run Code Online (Sandbox Code Playgroud)
我不知道为什么它总是解析为true,即使在传递字符时传递数字也是如此.