1 Attachment(s)
[DHTML]Configurable Date Validator
1) CODE TITLE: DateValidator.js
2) AUTHOR NAME/NOTES: Anton Chertok
3) DESCRIPTION: Automatic Date Validator. Allows the user to enter a date in a variety of dd/mm/yyyy formats, configurable by the "web-master" to:
- dd/mm/yyyy or mm/dd/yyyy date formats.
- "Correct" dates to be zero padded or not. i.e. 01/01/2007 or 1/1/2007
- Choice of default separator. e.g. 1/1/2007 or 1-1-2007 or 01012007
- Auto corrects users prefered separator to the default. So if the default separator is "-", 1/1/2007 will be converted to 1-1-2007 on validation.
- Allows the user to write their own error handing code, with easy access to the text field object causing the error.
- Comes with a default error message, and can be used as is without any extra coding.
- Automatically finds input text fields with "date" in their id, and attaches validation to them. e.g. <input type="text" id="myDateField">. Just need to include the .js and it is set up.
- Automatically puts in todays date when "t" is keyed.
- Uses the onblur event to validate on.
4) URL TO CODE: (this may work, but probably won't)
http://necrotic.ath.cx/proto/DateValidator.html
or, ATTACHED BELOW (see #3 in guidelines below):
Use this code to demo the js. Just put the following in a html file in the same directory as DateValidator.js, and it will all work.
Code:
<html>
<head>
<title>Date Validator Demo</title>
<script type="text/javascript" src="DateValidator.js"></script>
</head>
<body>
<h1>DateValidator.js Demo</h1><hr>
Start Date (dd/mm/yyyy): <input type="text" id="sDate" name="Start Date">
End Date (dd/mm/yyyy): <input type="text" id="e_date" name="End Date"><br>
Task: <input type="text" id="task" name="Task"><br>
<input type="button" id="dateInput" value="Some Button"><br><hr>
The two date fields are automatically picked up by the script.<br>
If you key "t" in a field, it will display todays date.<br>
Validation occurs when the field looses focus.
</body>
</html>
Add the following code to the HTML to see how user error handling works.
Code:
<script type="text/javascript">
function DateValidatorErrorHandler(oField)
{
var msg = "This is the user error handler.\nThe bad date value is " + oField.value;
alert(msg);
}
</script>
All in all, easy to use, and useful.