0

我想使用 java 脚本为范围验证器定义一个函数。我定义了它,但它只适用于整数。我的问题是最小值和最大值可以是什么?如果它们是字符串或其他类型,如何检查最小值和最大值?
这是我的功能:

function minmax(value) {
var min = document.getElementById("min_val").value;  
var max = document.getElementById("max_val").value;
if (min <= max) {
    if (parseInt(value) < min || isNaN(value)) {
        alert("input shouldn't less than  " + min);
    }
    else if (parseInt(value) > max) {
        alert("input shouldn't more than  " + max);
    }
}
else
    alert("The MaximumValue cannot be less than the MinimumValue  of RangeValidator");

}

4

1 回答 1

0

普通的旧 JavaScript 使用typeof运算符来确定变量或值的基础类型并返回该类型的字符串值,如下所示:

typeof 37 would return 'number'
typeof 3.14 would return 'number'
typeof "37" would return 'string'
typeof "" would return 'string'
typeof function() {} would return 'function'
typeof true would return 'boolean'
typeof false woulld return 'boolean'
typeof undefined would return 'undefined'
typeof {1, 2, 3} would return 'object'
typeof null would return 'object'

我相信您应该关心的唯一两种类型是'number'and 'string',因为您可以将 a 强制'string'转换为 a 'number'

我会在你的函数顶部进行检查,看看minandmax是否两者都是,'number'或者'string'如果不是,那么退出你的函数。

于 2013-06-27T22:16:12.757 回答