
i want to put alert on the text box that enter data should be in DD/MM/YYYY format only through javascript validation.

<html>
<script type='text/javascript'>
var dminyear = 1900;
var dmaxyear = 2200;
var sep= "/"
function checkNumber(str1){
var x;
for (x = 0; x < str1.length; x++){
var cr = str1.charAt(x);
if (((cr < "0") || (cr > "9")))
return false;
}
return true;
}
function getcharacters(s, sep){
var x;
var Stringreturn = "";
for (x = 0; x < s.length; x++){
var cr = s.charAt(x);
if (sep.indexOf(cr) == -1)
Stringreturn += cr;
}
return Stringreturn;
}
function checkFebruary(cyear)
{
return (((cyear % 4 == 0) && ( (!(cyear % 100 == 0)) || (cyear % 400 == 0))) ? 29 : 28 );
}
function finaldays(nr) {
for (var x = 1; x <= nr; x++) {
this[x] = 31
if (x==4 || x==6 || x==9 || x==11)
{
this[x] = 30}
if (x==2)
{
this[x] = 29}
}
return this
}
function dateValid(strdate)
{
var monthdays = finaldays(12)
var cpos1=strdate.indexOf(sep)
var cpos2=strdate.indexOf(sep,cpos1+1)
var daystr=strdate.substring(0,cpos1)
var monthstr=strdate.substring(cpos1+1,cpos2)
var yearstr=strdate.substring(cpos2+1)
strYr=yearstr
if (strdate.charAt(0)=="0" && strdate.length>1) strdatestrdate=strdate.substring(1)
if (monthstr.charAt(0)=="0" && monthstr.length>1) monthstrmonthstr=monthstr.substring(1)
for (var i = 1; i <= 3; i++) {
if (strYr.charAt(0)=="0" && strYr.length>1) strYrstrYr=strYr.substring(1)
}
pmonth=parseInt(monthstr)
pday=parseInt(daystr)
pyear=parseInt(strYr)
if (cpos1==-1 || cpos2==-1){
alert("The date format must be : dd/mm/yyyy")
return false
}
if (monthstr.length<1 || pmonth<1 || pmonth>12){
alert("Enter a valid month")
return false
}
if (daystr.length<1 || pday<1 || pday>31 || (pmonth==2 && pday>checkFebruary(pyear)) || pday > monthdays[pmonth]){
alert("Enter a valid day")
return false
}
if (yearstr.length != 4 || pyear==0 || pyear<dminyear || pyear>dmaxyear){
alert("Enter a valid 4 digit year between "+dminyear+" and "+dmaxyear)
return false
}
if (strdate.indexOf(sep,cpos2+1)!=-1 || checkNumber(getcharacters(strdate, sep))==false){
alert("Enter a valid date")
return false
}
return true
}
function validate(inputtxt)
{
var crdt=inputtxt.value
if (dateValid(crdt)==false)
{
document.form1.text1.focus()
return false;
}
return true;
}
</script>
<body onload='document.form1.text1.focus()'>
<form name="form1" action="#">
Enter a valid date [dd/mm/yyyy format] : <input type='text' name='text1'/>
<input type="submit" name="submit" value="Submit" onclick="validate(document.form1.text1)"/>
</form>
</body>
</html>
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.