Click here to Skip to main content
15,910,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the following function work properly in IE but Gives 'NaN' output in FireFox & opera
C#
function ageCount() {
    debugger;
    var age = 0;
    var AgeOF = 0;
    var date1 = new Date();
    var dob = document.getElementById('MainContent_txtDOB').value;
    var date2 = new Date(dob);
    var pattern = /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/; //Regex to validate date format (dd/mm/yyyy)
    if (pattern.test(dob)) {
        var y1 = date1.getFullYear(); //getting current year
        var y2 = date2.getFullYear(); //getting dob year
       age = parseInt(y1,10) - parseInt(y2,10);           //calculating age 
       //document.write("Age : " + age);is
       AgeOF = parseInt(age, 10);
       if(isNaN(AgeOF))
        {
            AgeOF = 0;
        }
        alert(AgeOF);

        document.getElementById('MainContent_txtAge').value = AgeOF;
        return true;
    } else {
        alert("Invalid date format. Please Input in (dd/mm/yyyy) format!");
        return false;
    }
}
Posted
v2

1 solution

I just tried your code - [Demo] javaScript Nan error in Firefox & opera & in IE work Properly[^].

I tested in latest IE, FireFox, Chrome and Opera.
It is working without any errors.
 
Share this answer
 
Comments
tupkar.anil 15-Jun-13 3:18am    
I handle the erroe If(NaN(ageOf)){AgeOf =0;}
check it once again
Yes, but it is not going inside the if condition, so no issues.
Can you give one example?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900