SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Issue in Displaying NaN values

    Hi..

    I created a function and I encountered problem because I got a NaN values. And I want to resolve it..I don't want to display NaN values. instead of NaN I just want it blank.

    here is my code:
    Code:
    function doz(oText){
        var P27_max = document.getElementById("P27_max").value;
        var P27_max_convert = parseFloat(P27_max) + parseFloat(1);   
        var P27_maxdoz = document.getElementById("P27_maxdoz").value = P27_max_convert;
        
        
        var P28_max = document.getElementById("P28_max").value;
        var P28_max_convert = parseFloat(P28_max) + parseFloat(2);
        var P28_maxdoz = document.getElementById("P28_maxdoz").value = P28_max_convert;
    }
    when I entered data in
    P27_max so the P27_maxdoz was computed but the problem is the P28_max has also value 'NaN'.

    How can I eliminate or did not display NaN

    Thank you

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Give the following a try, i simplified the process of how it collects and stores the value as well as a check to see whether the conversion leads to a number otherwise it adds a blank value.

    Code JavaScript:
    function doz(oText) {
        var value, convert, self,
            collect = {
                ['P27_max', 'P27_maxdoz', 1],
                ['P28_max', 'P28_maxdoz', 2]
            };
     
        for (var i = 0, max = collect.length; i < max; i++) {
            // Caching
            self = collect[i];
     
            value           = document.getElementById(self[0]).value;
            convert         = parseFloat(value) + self[2];
            window[self[2]] = document.getElementById(self[1]).value = (!isNaN(convert) ? convert : '');
        }
    }

  3. #3
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I resolved it using this code:

    Code:
        var P45_min = document.getElementById("P45_min").value;
        var P45_min_convert = parseFloat(P45_min) + parseFloat(13);
        if( !isNaN(P45_min_convert) ){
        var P45_mindoz = document.getElementById("P45_mindoz").value = P45_min_convert; 
        }
    Thank you


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •