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

Validate input. 1

Status
Not open for further replies.

shawkz

Programmer
Joined
Oct 25, 2005
Messages
84
Location
GB
Hi, i have a date/time text field which i submit to another page to use in an sql query.. i need to validate what is entered so it can be submited.

Example of entered date/time 02/05/2006 15:23

This is.. dd/mm/yyyy hh:nn

Need some way of checking that what is typed in matches the imput mask below...

dd = 01 to 31
mm = 01 - 12
yyyy = 2000 - 2010
hh = 00 - 23
nn = 00 - 59

Please can anyone help?

Regards,

SHawkz
 
Here's a regexp 1-liner that will ensure your date is inputted with the correct format. It will not check to ensure an invalid date such as november 31 is entered, but it will ensure that they input a date in the format you specified above. To add in the extra validation should not be too difficult after you have determined that the date format is correct.
Code:
<script type="text/javascript">

//This is.. dd/mm/yyyy hh:nn

//Need some way of checking that what is typed in matches the imput mask below...

//dd = 01 to 31
//mm = 01 - 12
//yyyy = 2000 - 2010
//hh = 00 - 23
//nn = 00 - 59

function validateDate(str) {
   if (/^(0[1-9]|[1-2]\d|3[0-1])\/(0[1-9]|1[0-2])\/(200\d|2010) ([0-1]\d|2[0-3])\:([0-5]\d)$/.test(str)) {
      alert("this is a valid date format");
   }
   else {
      alert("this date format is invalid");
   }
}

</script>

<body>
enter a date in the format dd/mm/yyyy hh:nn<br />
<input type="text" id="blah" />
<input type="button" value="validate" onclick="validateDate(document.getElementById('blah').value)" />
</body>

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
Thanks for that, works a treat! Your worth a star!!!

Kindest thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top