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

Date Validation

Status
Not open for further replies.
Aug 13, 2001
14
DK
I have to validate that in a date field, date only is entered WITHOUT TIME.

Is there a quick way to this.

Thanks,
Rajkumar
 
i have ten next script who validates the forms date but the script only works fine in explorer in netscape sends to me a error, i dont know where is the error here are the web page of test where i have the script try it may be can works for you
the date need to be in the next format dd/mm/yyyy if you dont put this format the date will be incorrect
<html>

<HEAD>
<META NAME=&quot;GENERATOR&quot; CONTENT=&quot;Microsoft FrontPage 5.0&quot;>
<META NAME=&quot;ProgId&quot; CONTENT=&quot;FrontPage.Editor.Document&quot;>
<TITLE>Validate Date Format</TITLE>

<SCRIPT>

function valida(strDate){
var datePat = /^(\d{2,2})(\/|-)(\d{2,2})\2(\d{4})$/;
var matchArray = strDate.match(datePat);

if (matchArray == null){
alert (&quot;La fecha debe tener el formato dd/mm/yyyy&quot;);
}

// var day = matchArray[1];
// var month = matchArray[3];

var day = matchArray[1];
var month = matchArray[3];
var year = matchArray[4];

if (day < 2 || day > 31) alert (&quot;La fecha debe tener el formato dd/mm/yyyy&quot;);
if (month < 2 || month > 12) alert (&quot;La fecha debe tener el formato dd/mm/yyyy&quot;);
if ((month == 4 || month == 6 || month==9 || month == 11) && day == 31) alert (&quot;La fecha debe tener el formato dd/mm/yyyy&quot;);
if (month == 2){
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day == 29 && !isleap)) alert (&quot;La fecha debe tener el formato dd/mm/yyyy&quot;);

}
return (&quot;La fecha esta en formato correcto&quot;);
}

</SCRIPT>

</HEAD>

<BODY BGCOLOR=&quot;#FFFFFF&quot;>

<P ALIGN=&quot;center&quot;>&nbsp;</P>
<H1 ALIGN=&quot;center&quot;><font color=&quot;#808080&quot;>Valida forma</font></H1>
<FORM NAME=&quot;DateForm&quot;>
<P ALIGN=&quot;center&quot;><INPUT TYPE=&quot;Text&quot; NAME=&quot;fecha&quot; SIZE=&quot;20&quot;><BR>
(dd/mm/yyyy or yyyy)<BR>

<P ALIGN=&quot;center&quot;><INPUT TYPE=&quot;Button&quot; VALUE=&quot;Validate&quot; ONCLICK=&quot;alert(valida(DateForm.fecha.value));&quot;></P>
</FORM>

</body>

</html>
 
here are the correct code a good friend help to me to solve this

<html>

<HEAD>
<META NAME=&quot;GENERATOR&quot; CONTENT=&quot;Microsoft FrontPage 5.0&quot;>
<META NAME=&quot;ProgId&quot; CONTENT=&quot;FrontPage.Editor.Document&quot;>
<TITLE>Validate Date Format</TITLE>

<SCRIPT>

function valida(strDate){
var datePat = /^(\d{2,2})(\/|-)(\d{2,2})\2(\d{4})$/;
var matchArray = strDate.match(datePat);

if (matchArray == null){
alert (&quot;La fecha debe tener el formato dd/mm/yyyy&quot;);
document.DateForm.fecha.value=&quot; &quot;;
document.DateForm.fecha.focus();
return (false);
}

// var day = matchArray[1];
// var month = matchArray[3];

var day = matchArray[1];
var month = matchArray[3];
var year = matchArray[4];

if (day < 2 || day > 31){ alert (&quot;Favor de introducir una fecha válida&quot;);
return(false);
}
if (month < 2 || month > 12){ alert (&quot;Favor de introducir una fecha válida&quot;);
return(false);
}
if ((month == 4 || month == 6 || month==9 || month == 11) && day == 31){ alert (&quot;Favor de introducir una fecha válida&quot;);
return(false);
}
if (month == 2){
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day == 29 && !isleap)){ alert (&quot;Favor de introducir una fecha válida&quot;);
return (false);
}
}


alert (&quot;La fecha esta en formato correcto&quot;);
return (true);

}

</SCRIPT>

</HEAD>

<BODY BGCOLOR=&quot;#FFFFFF&quot;>

<P ALIGN=&quot;center&quot;> </P>
<H1 ALIGN=&quot;center&quot;><font color=&quot;#808080&quot;>Valida forma</font></H1>
<FORM NAME=&quot;DateForm&quot;>
<P ALIGN=&quot;center&quot;><INPUT TYPE=&quot;Text&quot; NAME=&quot;fecha&quot; SIZE=&quot;20&quot;><BR>
(dd/mm/yyyy or yyyy)<BR>

<P ALIGN=&quot;center&quot;><INPUT TYPE=&quot;Button&quot; VALUE=&quot;Validate&quot; ONCLICK=&quot;return valida(DateForm.fecha.value);&quot;></P>
</FORM>

</body>

</html>
 
Hello Yordangs,

I appreciate your help. It works OK.

Could you please also guide me on how to have the default date appear in DD/MM/YY format?

Regards,
Rajkumar
 
the code seems a bit complex to me but that may be because everything in it is in spanish.

You could instead of using the regular expression use the split() method for strings.

var date = &quot;6/10/1978&quot;.split(&quot;/&quot;);
var day = date[0];
var month = date[1];
var year = date[2];

// then here you would do the rest of the check.

To answer your question ramrakhyani there is a way to get the two digit value of the year.

For example :

var today = new Date(); // when there are not arguments it takes current date

var myStringDate = today.getDate() + &quot;/&quot; + (today.getMonth+1) + &quot;/&quot; today.getYear();

However if you wanted the four digit year :

var myStringDate = today.getDate() + &quot;/&quot; + (today.getMonth+1) + &quot;/&quot; today.getFullYear();

The difference is in the getFullYear() or getYear() methods that are part of the Date object that comes with JavaScript.

I hope this helps. Gary Haran
 
Hello Yordangs,

Further to my e-mail dated 23 April 2002, I found out an error in the Date validation function.

If once the date is validated as false, then valida(strDate)continues to return false even after the date has been corrected.

Could you please help on this.

Thanks,
Raj
 
Although you certainly can validate a date it is better to force the user to enter a valid date in the first place, which I think was what Xutopia was getting at in the first of his two posts.
HTH,
Clive
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top