validering af dato og text-input
Følgende script validerer på dato for gyldighed, hvilket fungere perfekt:<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function isValidDate(dateStr) {
// Checks for the following valid date formats:
// dd-mm-yyyy dd.mm.yyyy
// Also separates date into day, month, and year variables
var datePat = /^(\d{1,2})(\.|-)(\d{1,2})\2(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("Date is not in a valid format.")
return false;
}
// parse date into variables
day = matchArray[1];
month = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days.")
return false
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days.");
return false;
}
}
var toDay = new Date()//sets today date
var newDate = new Date(year,month-1,day)//sets the new input date
var dif = newDate-toDay;//difference in miliseconds
dif = Math.ceil(dif/1000/60/60/24);//difference in days
if(dif<0){//if the date is in the past
alert('The date is in the past. Please input a valid date!');
//document.forms[0].elements['date'].value='';
return false;
}
return true; // date is valid
}
// End -->
</SCRIPT>
<FORM ACTION="" METHOD="post" onSubmit="return isValidDate(this.date.value);">
Skriv dato: <INPUT TYPE="text" NAME="date" SIZE="10" MAXLENGTH="10"> (dd.mm.åååå)
<INPUT TYPE="submit" VALUE="Dato"><BR>
Problemet er, at jeg forsøgt på at tilføje et obligatorisk text-felt (headline). Et eller andet går galt, for der sker intet, dvs. ej heller dato-valideringen virker længere. Dette script virker altså ikke:
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function isValidDate() {
if (myForm.headline.match.value == null){
alert("Headline is missing.");
return false;
}
// Checks for the following valid date formats:
// dd-mm-yyyy dd.mm.yyyy
// Also separates date into day, month, and year variables
var datePat = /^(\d{1,2})(\.|-)(\d{1,2})\2(\d{4})$/;
var matchArray = myForm.date.match.value(datePat); // is the format ok?
if (matchArray == null) {
alert("Date is not in a valid format.")
return false;
}
// parse date into variables
day = matchArray[1];
month = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days.")
return false
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days.");
return false;
}
}
var toDay = new Date()//sets today date
var newDate = new Date(year,month-1,day)//sets the new input date
var dif = newDate-toDay;//difference in miliseconds
dif = Math.ceil(dif/1000/60/60/24);//difference in days
if(dif<0){//if the date is in the past
alert('The date is in the past. Please input a valid date!');
//document.forms[0].elements['date'].value='';
return false;
}
return true; // date is valid
}
// End -->
</SCRIPT>
<FORM ACTION="" METHOD="post" onSubmit="return isValidDate();">
Skriv dato: <INPUT TYPE="text" NAME="date" SIZE="10" MAXLENGTH="10"> (dd.mm.åååå)<BR>
Tekst: <TEXTAREA COLS="70" ROWS="2" NAME="headline" WRAP=virtual></TEXTAREA><BR>
<INPUT TYPE="submit" VALUE="Dato"><BR>
</FORM>
