有一个网页 - 它有一个文本框 - 我想强迫它只接受数字。我知道有很多选择,但我想非常具体地围绕什么是数字,什么不是 - 依赖于语言环境。所以我写了一些我在JS中用于其他语言的代码。它是少数几个以同样方式失败的检查功能之一。
1 function IsNumber( keyCode ){
2 //Allow numbers 0 to 9 and nothing else
3
4 // Ugly but adding zero forces the keyCode to be a number
5 switch (keyCode + 0){
6 case ( (keyCode >= ASC_ZERO) && ( keyCode <= ASC_NINE) ):
7 return true;
8
9 default:
10 return false;
11 }
12}
keycode = 49,ASC_ZERO = 48,ASC_NINE = 57
代码评估第6行,当它应该执行第7行时,它将下降到第10行
谷歌控制台检查出我的所有鸭子连续,所以它应该工作。
Google控制台输出。
>keyCode <= ASC_NINE
>true
>keyCode >= ASC_ZERO
>true
>true && true
>true
>( (keyCode >= ASC_ZERO) && ( keyCode <= ASC_NINE) )
>true
>(keyCode + 0)
>49
>( (keyCode >= ASC_ZERO) && ( keyCode <= ASC_NINE) )
>true
我有双重检查括号,我认为我的分号都是正确的。
我可以把它作为一系列if else语句来做 - 是的,但是为了保持我的所有代码一致,并且因为其他检查函数有很多情况,我觉得Switch Case更易于维护。
现在我只想知道为什么它不起作用。有什么想法吗?
要重新创建我的问题,请在第一个文本字段中输入一个数字,然后点击该事件。
Total Testong Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>increment field</title>
</head>
<body>
<script type="text/javascript">
// START OF DECLARATIONS
const ASC_UPPER_A = 65
const ASC_UPPER_Z = 90
const ASC_LOWER_A = 97
const ASC_LOWER_Z = 122
const ASC_ZERO = 48
const ASC_NINE = 57
const ASC_FULLSTOP = 46
const ASC_NEGATIVE = 45
const KEY_LEFT = 37
const KEY_RIGHT = 39
const KEY_UP = 38
const KEY_DOWN = 40
const KEY_DELETE = 46
const KEY_BACKSPACE= 8
const KEY_RETURN=13
const KEY_TAB = 9
const TYPE_IS_NUMERIC = 1
const TYPE_IS_NUMBER = 2
const TYPE_IS_ALPHA = 4
const TYPE_IS_DECIMAL = 8
const TYPE_IS_INTEGER = 16
const TYPE_IS_WHITESPACE = 32
// DECLARE HELPER FUNTIONS
function IsNumber( keyCode ){
//Allow numbers 0 to 9 and nothing else
// Ugly but adding zero forces the keyCode to be a number
switch (keyCode + 0){
case ( (keyCode >= ASC_ZERO) && ( keyCode <= ASC_NINE) ):
return true;
default:
return false;
}
}
function IsNumeric( keyCode ){
// We aren't using is_Numeric as it will
// allow through e, ^, % and a couple of other chars
//Check for NEGATIVE (-) & ASC_FULLSTOP (.) We don't allow comma (,) as it is not in the NZ locale
// Ugly but adding zero forces the keyCode to be a number
switch (keyCode + 0){
case IsNumber( keyCode ):
case ASC_FULLSTOP:
case ASC_NEGATIVE:
return true;
default:
return false;
}
}
function IsInteger( keyCode ){
switch ( keyCode + 0){
case IsNumber(keyCode):
return true;
case ASC_NEGATIVE:
return true; // Negative integer
default:
return false;
}
}
function IsAlpha( keyCode ){
// We aren't using ctype_alpha as it will allow through
// chars not in the NZ_Locale also numbers from -128 to 255
// are considered alpha of a single char
// THIS TEST WILL ONLY WORK FOR SINGLE CHARS
switch ( keyCode + 0){
case (( keyCode >= ASC_LOWER_A ) && ( keyCode <= ASC_LOWER_Z ) ):
case (( keyCode >= ASC_UPPER_A ) && ( keyCode <= ASC_UPPER_Z ) ):
return true;
default:
return false;
}
}
function IsWhiteSpace( keyCode ){
switch ( keyCode + 0){
case ( ASC_SPACE ):
return true;
default:
return false;
}
}
// Main function to test a if key stroke is valid
function AllowKey(e, keyType ) {
var unicode=e.charCode? e.charCode : e.keyCode
// If movement key then return without further tests
switch( unicode){
case unicode >=KEY_LEFT && unicode <=KEY_DOWN:
case KEY_DELETE:
case KEY_BACKSPACE:
case KEY_RETURN:
case KEY_TAB:
return true;
default:
break;
}
// Test if unicode / keyCode passes one of the tests
if ( keyType & TYPE_IS_NUMERIC ) {
if (IsNumeric( unicode )){
return true;
}
}
if ( keyType & TYPE_IS_NUMBER ) {
if (IsNumber( unicode )){
return true;
}
}
if ( keyType & TYPE_IS_ALPHA ) {
if (IsAlpha( unicode )){
return true;
}
}
if ( keyType & TYPE_IS_DECIMAL ) {
if (IsNumber( unicode )){
return true;
}
}
if ( keyType & TYPE_IS_INTEGER ) {
if (IsInteger( unicode )){
return true;
}
}
if ( keyType & TYPE_IS_WHITESPACE ) {
if (IsWhiteSpace( unicode )){
return true;
}
}
// If we have made it this far the unicode keystoke has failed the test
return false;
}
</script>
<form>
<p>
<label> TYPE_IS_NUMBER - 0 to 9 only </label>
<input type="text" size=18 onkeypress="return AllowKey(event, TYPE_IS_NUMBER )">
</p>
<p>
<label> TYPE_IS_NUMERIC - 0 to 9, - and . only </label>
<input type="text" size=18 onkeypress="return AllowKey(event, TYPE_IS_NUMERIC )">
</p>
<p>
<label> TYPE_IS_ALPHA ONLY a to z an A to Z only </label>
<input type="text" size=18 onkeypress="return AllowKey(event, TYPE_IS_ALPHA )">
</p>
<p>
<label> TYPE_IS_ALPHA (a to z an A to Z ) and TYPE_IS_WHITESPACE ' " only </label>
<input type="text" size=18 onkeypress="return AllowKey(event, TYPE_IS_ALPHA + TYPE_IS_WHITESPACE )">
</p>
<p>
<label> TYPE_IS_ALPHA (a to z an A to Z ) and TYPE_IS_WHITESPACE ' " & TYPE_IS_NUMERIC only </label>
<input type="text" size=18 onkeypress="return AllowKey(event, TYPE_IS_ALPHA + TYPE_IS_WHITESPACE + TYPE_IS_NUMERIC)">
</p>
</form>
</body>
</html>
答案 0 :(得分:1)
您正在检查(keyCode + 0)的值(keyCode&gt; = ASC_ZERO)&amp;&amp; (keyCode&lt; = ASC_NINE)
喜欢
if( (keyCode + 0) == (keyCode >= ASC_ZERO) && ( keyCode <= ASC_NINE) )
这样就是错误。