Hi
I have my date in the format dd/mm/yyyy (text box). How do i validate
the date field using javascript
Thanks in advance
Dhanavel
Sent via Deja.com http://www.deja.com/
Before you buy.
Hi,
You may start with creating a regular expression pattern to separate string
dd/mm/yyyy into array 'dd','mm','yyyy':
var datePat = /^(.+)\/(.+)\/(.+)$/;
var formvalue = document.FormName.FieldName.value; // this is where you
will have to enter FormName and FieldName
var dateArray = formvalue.match(datePat);
if (dateArray == null) {
alert("You entered invalid date. Please try again..."); // This alert
will pop up if slashes are missing
return false;
}
var dayValue = dateArray[1];
var monthValue = dateArray[2];
var yearValue = dateArray[3];
var generalPat = /\D/; // each value must be a number. This pattern
will check if there are other characters than digits
var dayCheck = dayValue.match(generalPat);
var monthCheck = monthValue.match(generalPat);
var yearCheck = yearValue.match(generalPat);
if ((dayCheck != null) || (monthCheck != null) || (yearCheck != null)) {
alert("Please enter a valid date in form 'dd/mm/yyyy'");
return false; // return false if the date contained characters
other than digits
}
if ((dayValue > 31) || (monthValue > 12) || (yearValue < 1900) || (yearValue
> 2000)) {
alert("You entered invalid date. Please try again..."); // This alert
will pop up if numbers are too big
return false;
}
BTW. I'm just working on a e-mail form validator, so I wrote this one for
you because it's much easier task. Please make sure that you remove
line-breaks inserted by Outlook. "if" statements are on a single line, and
comments as well. You may use this code in a function, and then call it with
"onSubmit" event.
~ Sascha.
My opinions may have changed, but not the fact that I am right.
(Ashleigh Brilliant)
Can someone, please, give a shorter expression than the one below for
checking dates?
++++++++++++++++++++++++++++++++++++++++
var reDate =
/^((29(-|\/)0?2(-|\/)(19(0[48]|[2468][048]|[13579][26])|2000))|(((3[01]|[12][0-9]|0?[1-9])(-|\/)(0?[13578]|1[02]))|((30|[12][0-9]|0?[1-9])(-|\/)(0?[469]|11))|(([12][0-8]|19|0?[1-9])(-|\/)(0?2)))(-|\/)(19\d{2}|2000))$/
function testInput(re, fld){
if(!re.test(fld.value)){
// code for invalid case
return false
}else{
// code for valid case
return true
}
}
testInput(reDate, txtDate)
++++++++++++++++++++++++++++++++++++++++
On Wed, 8 Nov 2000 12:48:40 +0100, "Aleksandar V." <aca...@yahoo.com>
wrote:
HoloGuides : We are glad to be of service.
http://www.geocities.com/holonet2001
dhan...@my-deja.com wrote in message <8ub399$4ab$1...@nnrp1.deja.com>...
>
>
>Hi
>
>I have my date in the format dd/mm/yyyy (text box). How do i validate
>the date field using javascript
>
>Regular expressions can be quite long and can check for all cases:
>months with 28, 29, 30, 31 days and valid and invalid leap year dates.
>
>Can someone, please, give a shorter expression than the one below for
>checking dates?
Please answer after what you quote. Please trim your quotes. Please
read <URL: http://www.merlyn.demon.co.uk/js-dates.htm#valid>, other
parts of the page, and js-tests.htm.
Use .split('/') or .split('-') to break a string date into three;
convert each part to a number; then
function ValidDate(y, m, d) // m = 0..11
{ with (new Date(y, m, d))
return ((getDate()==d) && (getMonth()==m)) }
I suppose a RegExp, though complex, might be better id the need is
solely to validate strings; but if the date is to be used it's likely
that it will need to be converted to numbers anyway
--
Š John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v4.00 MIME. Š
Web <URL: http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS, EXE in <URL: http://www.merlyn.demon.co.uk/programs/> - see 00index.txt.
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm &c.