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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validate date

Status
Not open for further replies.

taz6053

Programmer
Feb 12, 2007
9
US
Can anyone show me a simple way to validate a date textfield in a form.

Thanks
 
Here is a date validation function using a regular expression.
Code:
function date_format(value) { 
  //Accepts 1 or 2 digit day/month fields and 2 or 4 digit year fields.  Accpets - . and / as date separators.
//  var re = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/; //four digit years only.
  var re = /^\d{1,2}(\-|\/|\.)\d{1,2}\1(\d{2}|\d{4})$/; //Accepts 2 or 4 digit years.
  return re.test(value);
}

Call the function passing in the date and it returns true or false.
It allows for dash, dot and forward slash as separator characters and 2 or 4 digit years. If you want only four digit years just uncomment the top var re line and comment out the bottom.


At my age I still learn something new every day, but I forget two others.
 
Below is what I have so far. The problem is that even though I get a return of 'true', I am still getting that the date is not valid. I also need to be able to use a date in the format of 1/2 and have the script reformat it to 1/2/2007. Thanks again for your help.

//validate date field
function date_format(value) {
//Accepts 1 or 2 digit day/month fields and 2 or 4 digit year fields. Accpets - . and / as date separators.
//var re = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/; //four digit years only.
var re = /^\d{1,2}(\-|\/|\.)\d{1,2}\1(\d{2}|\d{4})$/; //Accepts 2 or 4 digit years.
//return re.test(value);
var sResult=re.test(value);
alert(sResult)
if (sResult="false") {
alert("Date not in valid format.")}
else {
alert("Date format accepted.")}
}
 
When I have a form with dates, I use month, date, and year <select>s with the possible dates. That prevents a LOT of potential user error, and you get the date formatted exactly the way you want.

Lee
 
Are you talking about 3 seperate drop down boxes? That really isn't feasible in this situation.
 
Yes, that's what I use. I've not found a situation where it was any less feasible to do that than validate error-filled user input.

Lee
 
You need == rather than = in your if statement.
Code:
if (sResult=[red]=[/red]"false") {
      alert("Date not in valid format.")}
  else     {
  alert("Date format accepted.")}

At my age I still learn something new every day, but I forget two others.
 
Thanks that helped. One last thing (I hope). How would I set the form field back to blank when they enter an incorrect date format?
 
document.getElementById('MyFieldName').value = '';

Assuming of course that your field has an ID tag. If it does not, then you should give it one as this is the best standards based way to address the field object.

At my age I still learn something new every day, but I forget two others.
 
Is there a way for the javascript to know what field name triggered the function. I would like to keep this nonspecific so that I can use the same function for more than one textfield on the same form.
 
If you use something like:
Code:
function date_format(oneinput)
{
var value=oneinput.value;
/*
rest of your code here
*/
if (sResult=="false")
 {
 alert("Date not in valid format.")
 oneinput.value='';
 }
else
 {
 alert("Date format accepted.")
 }
}
and
Code:
<input type="text" name="date" onchange="date_format(this)">
the function will work with any text input like you want.

Lee
 
trollacious is spot on about the best way to handle dates.

Especially if you're dealing across locales and 3/12/2007 could be either the third of December or the twelfth of March depending on the accepted style where the user lives.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Enable Apps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top