La funzione split e replace le ho fatto al di sotto di questa funzione.
Il tutto funziona correttamente ma se scrivo 29 febbraio 21010 questa funzione lo accetta invece non è corretta
è solo questo il mio problema
codice:
function IsDate(txtDate)
{
strData = Trim(txtDate);
var stringa = Split(txtDate, "-");
var giorno = stringa[0];
var mese = stringa[1];
var anno = stringa[2];
var boolCtr = true;
if(giorno.length==1)
giorno = "0"+giorno;
if(mese.length==1)
mese = "0"+mese;
if(anno.length==2)
boolCtr = false;
txtDate = giorno +"/" + mese +"/" + anno
txtDate = Trim(txtDate);
alert("*" +txtDate+ "*")
try
{
if (txtDate.length != 10)
{
boolCtr = false;
}
else if
(
isNaN(txtDate.substring(0, 2)) ||
txtDate.substring(2, 3) != "/" ||
isNaN(txtDate.substring(3, 5)) ||
txtDate.substring(5, 6) != "/" ||
isNaN(txtDate.substring(6, 15))
)
{
boolCtr = false;
}
else if (
txtDate.substring(3, 5)==4 ||
txtDate.substring(3, 5)==6 ||
txtDate.substring(3, 5)==9 ||
txtDate.substring(3, 5)==11
)
{
if (txtDate.substring(0, 2)>30)
boolCtr = false;
}
else if (
txtDate.substring(3, 5)==2
)
{
if (txtDate.substring(0, 2)>29)
boolCtr = false;
}
if (
txtDate.substring(3, 5)==1 ||
txtDate.substring(3, 5)==3 ||
txtDate.substring(3, 5)==5 ||
txtDate.substring(3, 5)==7 ||
txtDate.substring(3, 5)==8 ||
txtDate.substring(3, 5)==10 ||
txtDate.substring(3, 5)==12
)
{
if (txtDate.substring(0, 2)>31)
boolCtr = false;
}
return boolCtr;
}
catch (e)
{
return boolCtr;
}
}
function Trim(StringToTrim) {
// CONTROLLA CHE IL VALORE IN INPUT SIA DI TIPO STRING
if (typeof(StringToTrim) != "string") { return StringToTrim; }
// CATTURA IL PRIMO CARATTERE DELLA STRINGA PER CONTROLLARE CHE NON SIA UNO SPAZIO VUOTO
var StringBlank = StringToTrim.substring(0, 1);
// ELIMINA LO SPAZIO VUOTO DALLA PRIMA POSIZIONE DELLA STRINGA
while (StringBlank == " ") {
StringToTrim = StringToTrim.substring(1, StringToTrim.length);
StringBlank = StringToTrim.substring(0, 1);
}
// CATTURA L'ULTIMO CARATTERE DELLA STRINGA PER CONTROLLARE CHE NON SIA UNO SPAZIO VUOTO
StringBlank = StringToTrim.substring(StringToTrim.length - 1, StringToTrim.length);
// ELIMINA LO SPAZIO VUOTO DALL'ULTIMA POSIZIONE DELLA STRINGA
while (StringBlank == " ") {
StringToTrim = StringToTrim.substring(0, StringToTrim.length-1);
StringBlank = StringToTrim.substring(StringToTrim.length-1, StringToTrim.length);
}
// ELIMINA POTENZIALI SPAZI VUOTI MULTIPLI ALL'INIZIO ED ALLA FINE DI UNA STRINGA
while (StringToTrim.indexOf(" ") != -1) {
StringToTrim = StringToTrim.substring(0, StringToTrim.indexOf(" "));
StringToTrim += StringToTrim.substring(StringToTrim.indexOf(" ") + 1, StringToTrim.length);
}
// RESTITUISCE IL VALORE FINALE SENZA SPAZI VUOTI DI CONTORNO
return StringToTrim;
}
function Replace(StringToReplace, StringToChange, StringChangedIn)
{
return StringToReplace.replace(StringToChange, StringChangedIn);
}
function Split(StringToSplit, CharToSplit) {
return StringToSplit.split(CharToSplit);
}