Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NaN problem

Status
Not open for further replies.

MorganGreylock

Programmer
Jan 30, 2001
223
US
I've got a date field that is completely optional for the user. If they DO enter a date, I want to make sure that 1) it is a valid date in the form mm/dd/yyyy and 2) that it is not in the past. (Today is ok, yesterday is not).

Checking for 2) is okay, I've got that part finished. The problem I'm having is with 1), where I'm using isNaN() to see if its a valid date, but if the user leaves the (optional) field blank, it complains that its not a valid number. Here's my code:
Code:
	if (isNaN(str)){
    	alert('That is not a valid date.');
		document.srdetails.date_needby.value = '';
		return false;
	}

Obviously there is a part missing, but I'm not sure what to add. Adding a check for && item.length > 0 doesn't work either. Any suggestions?

Thanks in advance
 
Try this to test if field is empty:

Code:
document.srdetails.date_needby.value.length == 0;

There's always a better way. The fun is trying to find it!
 
I've already tried this:

Code:
	if (isNaN(str) && document.srdetails.date_needby.length > 0){
    	alert('That is not a valid date.');
		document.srdetails.date_needby.value = '';
		return false;
	}

And that still doesnt work correctly.
 
Basically here is my problem. I am trying two different things and getting two different problems.

Case 1:
Code:
	if (isNaN(str)){
    	alert('That is not a valid date.');
		document.srdetails.date_needby.value = '';
		return false; }

The above code segment works correctly, except that an empty date should be allowed, and its not allowing an empty date.(The field is optional).

Case 2:

Code:
	if (isNaN(str) && document.srdetails.date_needby.length > 0){
    	alert('That is not a valid date.');
		document.srdetails.date_needby.value = '';
		return false;
	}

This piece of code now allows for a null date, but now anything that is not a valid date (like 'asdf' doesn't get trapped. Its interesting to me that I'm toggling between these two solutions, but both of them create different, and mutually exclusive problems.

Any help is greatly appreciated.
 
Try reversing the order of the two conditions in your if statement.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
As your code is now, it won't do anything except throw a javascript error. First, to test for length, you have to use this syntax:
Code:
document.srdetails.date_needby.[b]value[/b].length

Then, change the logic of your test:
Code:
if (document.srdetails.date_needby.value.length == 0){
    alert("Please enter a valid date");
    document.srdetails.date_needby.focus();
    return false;}
else if (isNaN(str)){
         alert("You have entered an invalid date");
         document.srdetails.date_needby.value = ""; 
         document.srdetails.date_needby.focus();
         return false;}

The first "if" tests for an empty text box value. The second one tests is str is a number. Of course, neither of these will test for a valid date, or any date for that matter. To test for a date, you'll have to break down the value of str into the parts of a date (month, day, year) and test each against the current date. May I suggest that instead of having the user enter a date in mm/dd/yyyy format, try using select boxes for the month, day and year. Then, testing for a valid date becomes far less cumbersome and you don't have to test for empty text boxes or isNaN.

There's always a better way. The fun is trying to find it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top