开发者

Datatype for "NaN" in validation process

Goal:

Evaluate a specifik value in my validation code

Problem:

The variable "currentValue" can retrieve a value named "NaN". And i don't know what kind of datatype that shall be applied in the my validation process.

Based on my previous test, NaN is neither a string or int or something similiar.

What datatype is NaN?

    var ddd = $('#field_timmar').val();
    //datac= $('#field_timmar').val();
    //len = currentValue.length;

    var currentValue = parseInt(ddd);


    // Validating message
 开发者_StackOverflow   if(currentValue <= 0)
    {
        $('#field_timmar_error1').show();

        $('#field_timmar_error2').hide();
        $('#field_timmar_error3').hide();

        nonError = false;
    }
    else if(currentValue == "NaN" && ddd != "")
    {
        $('#field_timmar_error2').show();

        $('#field_timmar_error1').hide();
        $('#field_timmar_error3').hide();

        nonError = false;
    }
    else if(currentValue == "NaN" && ddd == "")
    {
        $('#field_timmar_error3').show();

        $('#field_timmar_error1').hide();
        $('#field_timmar_error2').hide();

        nonError = false;
    }       
    else
    {
        $('#field_timmar_error1').hide();
        $('#field_timmar_error2').hide();
        $('#field_timmar_error3').hide();
    }   


The data type of NaN is actually Number, but because of its special status, it cannot be directly compared with any other value. There is a function isNaN that lets you test whether a number is NaN:

if (isNaN(currentValue)) {
  ...
}


NaN is a Number which is NaN. When cast to a String, it generally outputs "NaN", but it does not equal "NaN". NaN equals nothing, including NaN.

Simple enough for you?

The easiest way to test for NaN is the method isNaN.

console.log( isNaN( Number( "quack" ) ) ) // true
console.log( isNaN( Number( "1" ) ) ) // false

I once encountered an implementation glitch where NaN where generated through bad concatenation: 1 + "threeve" when what is wanted is "1" + "threeve", other times it is through situations like exampled above, or situations like the following:

// value is undefined
var obj = {}
console.log( 1 + obj.notDefinedProperty )
var arr = []
console.log( 1 + arr[ 1 ] )

// non-castable
var obj = {}
obj++;// also works for +=
console.log(obj)
"string" * 1


Other way to check if value is Nan ?

value !== value


NaN means not-a-number.you can test its with isNaN() function:

console.log( isNaN(123) );
console.log( isNaN("ABC") );

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜