if value is between two numbers

前端 未结 5 2067
无人共我
无人共我 2020-12-18 23:39

I want to be able to test whether a value is within a number range. This is my jQuery code...

if ((year < 2099) && (year > 1990)){
    return \         


        
5条回答
  •  爱一瞬间的悲伤
    2020-12-18 23:57

    You could make your own method:

    // jquery
    $(function() {
        var myNumber = 100;
        try {
            if (myNumber.isBetween(50, 150)) 
                alert(myNumber + " is between 50 and 100.");
            else 
                alert(myNumber + " is not between 50 and 100.");
        } catch (e) {
            alert(e.message());
        }
    
    });
    
    // js prototype
    if (typeof(Number.prototype.isBetween) === "undefined") {
        Number.prototype.isBetween = function(min, max, notBoundaries) {
            var between = false;
            if (notBoundaries) {
                if ((this < max) && (this > min)) between = true;
                alert('notBoundaries');
            } else {
                if ((this <= max) && (this >= min)) between = true;
                alert('Boundaries');
            }
            alert('here');
            return between;
        }
    }
    

    hope this helps.

    Max

提交回复
热议问题