Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Javascript function returns NaN

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Location
    Kochi,India
    Posts
    18
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Javascript function returns NaN

    sorry folks if this post is not pertaining to jsp. I could'nt find a forum for javascript in here. so I am posting it here.

    I've a jsp with a <html:text> element which is supposed to accept year values only.
    I've added an onblur js function to check if it is the correct entry.

    My Js function is as follows:
    ]
    <script type="text/javascript" >
    function isYear(yrval)
    {
     
    	if (yrval==null || yrval=="" || yrval=="0")
    	{
    		alert("Invalid value in Year!!");
    		return false;
    	}
    	var nyr=Number(yrval);
    	alert(yrval);
    	alert(nyr);
    	var curdt = new Date();
    	var cy = curdt.getFullYear();
    	alert(cy);
    	if (nyr>cy)
    	{
    		alert("Calendar Year cannot be greater than "+cy);
    		return false;
    	}
    	if (nyr)<(cy-1))
    	{
    		alert("You cannot enter data prior to year "+cy-1);
    		return false;
    	}
    	return true;
    }
    now my jsp looks as follows:
    HTML Code:
    <body>
    <div id="content">
    <center>
    <html:form action="/Savedata">
    <table id="mytbl" >
    	<tr><td class="tbhr" colspan="2">YEAR</td></tr>
    	<tr><td>Select Year:</td><td><html:text name="myvo" property="calYear" size="5" maxlength="4" onkeypress="return onlyNumbers();" onblur="return isYear(this.value);"/></td></tr>
    </html:form>
    </center>
    </div>
    </body>
    Now the problem : The alerts in js function is showing as NaN. Even for
    valid inputs, it's displaying the same message.

    Am i missing something here?


  2. #2
    Junior Member
    Join Date
    Apr 2011
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Javascript function returns NaN

    The value you are passing to isyear function is not a number.

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Javascript function returns NaN

    You might need to do something like this.

    var cy = new Number(curdt.getFullYear());

Similar Threads

  1. Space Monkey Returns
    By SPACE MONKEY in forum Member Introductions
    Replies: 1
    Last Post: February 17th, 2011, 09:59 AM
  2. getTimeInMillis() from the Calendar class returns wrong values
    By 16mydream in forum Java Theory & Questions
    Replies: 2
    Last Post: February 16th, 2011, 01:29 PM
  3. javascript problem
    By kundan_101 in forum Member Introductions
    Replies: 2
    Last Post: December 30th, 2010, 09:02 AM
  4. How web service returns xml?
    By nikos in forum Web Frameworks
    Replies: 2
    Last Post: October 7th, 2010, 12:50 PM
  5. How can i put session in Javascript?
    By rajani in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: July 8th, 2009, 12:46 PM