如何工作范围验证器?

时间:2013-06-27 21:32:36

标签: javascript asp.net rangevalidator

我想使用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");

}

1 个答案:

答案 0 :(得分: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''string',因为您可以强制'string'转换为'number'

我会在您的函数顶部放置一个检查,以查看minmax'number'还是'string',如果没有,那么就挽救你的功能。