| |||||||||||
Data Entry ValidationChecking for valid date inputThe following is a sample validation routine that checks for a valid date value from a text field entry. Enter any date in m/d/y or m-d-y short date format, or in long date format as in '25 May 2000', which can be entered with the year, month, and day in any order. If the input is valid, a Date object is successfully returned. If the function returns NaN, it indicates that the object does not represent a specific instance of time, or is not valid as a date value. The above form's validation handler uses isNaN to check this return value. Now let's explore how that is done. In cases where a string input is given for date values, as in a text box, the string is parsed according to the rules in the Date.parse method. There are several rules that govern what the parse method can successfully parse, the more important of which are:
Note that if the value of an argument is greater than its range or is a negative number, the stored values are modified accordingly. For example, if you specify The code for that exercise is as follows: <form name="theForm" onsubmit="return getDateDemo ( )">
<input name="dater">
<input type="submit" value="Get Date">
</form>
<script language="JavaScript">
<!--
function getDateDemo ( ) {
var d = document.theForm.dater.value;
d = new Date ( d );
if ( isNaN ( d ) ) {
alert ( 'Sorry, not a valid date' );
document.theForm.dater.select ( );
document.theForm.dater.focus ( );
return false
}
alert ( d );
return true;
}
//-->
</script>
In actual use, the alert after a valid input is not necessary. It is just there to show that a Date object is indeed returned if successful. Here is another sample that gets the day of week from a given text input. As long as the input is valid according to the rules of the parse method, it will successfully be converted to a valid Date object. Enter your birthday and see what day you were born. See AlsoValidating Against a Data Type ( Web Forms ) Web Forms Validation |
| ||||||||||
Check out related books at Amazon
© 2000-2008 Rey Nuñez All rights reserved.
If you have any question, comment or suggestion
about this site, please send us a note
You can help support aspxtreme