JavaScript DHTML/Development/Date Validation

Материал из Web эксперт
Версия от 07:19, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Date Format Validator

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Date Format Validator</title>
<script language="JavaScript">
// Date Format Validator
// Sample Format : 23 Feb 1983
// (c) 2002 Premshree Pillai
// http://www.qiksearch.ru
// qiksearch@rediffmail.ru
function validate_date(formName, textName)
{
 var errMsg="", lenErr=false, dateErr=false;
 var testObj=eval("document." + formName + "." + textName + ".value");
 var testStr=testObj.split(" ");
 if(testStr.length>3 || testStr.length<3)
 {
  lenErr=true;
  dateErr=true;
  errMsg+="There is an error in the date format.";
 }
 var monthsArr = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug" ,"Sep", "Oct", "Nov", "Dec");
 var daysArr = new Array;
 for (var i=0; i<12; i++)
 {
  if(i!=1)
  {
   if((i/2)==(Math.round(i/2)))
   {
    if(i<=6)
    {
     daysArr[i]="31";
    }
    else
    {
     daysArr[i]="30";
    }
   }
   else
   {
    if(i<=6)
    {
     daysArr[i]="30";
    }
    else
    {
     daysArr[i]="31";
    }
   }
  }
  else
  {
   if((testStr[2]/4)==(Math.round(testStr[2]/4)))
   {
    daysArr[i]="29";
   }
   else
   {
    daysArr[i]="28";
   }
  }
 } 
 var monthErr=false, yearErr=false;
 if(testStr[2]<1000 && !lenErr)
 {
  yearErr=true;
  dateErr=true;
  errMsg+="\nThe year \"" + testStr[2] + "\" is not correct.";
 }
 for(var i=0; i<12; i++)
 {
  if(testStr[1]==monthsArr[i])
  {
   var setMonth=i;
   break;
  }
 }
 if(!lenErr && (setMonth==undefined))
 {
  monthErr=true;
  errMsg+="\nThe month \"" + testStr[1] + "\" is not correct.";
  dateErr=true;
 }
 if(!monthErr && !yearErr && !lenErr)
 {
  if(testStr[0]>daysArr[setMonth])
  {
   errMsg+=testStr[1] + " " + testStr[2] + " does not have " + testStr[0] + " days.";
   dateErr=true;
  }
 }
 if(!dateErr)
 {
  eval("document." + formName + "." + "submit()");
 }
 else
 {
  alert(errMsg + "\n____________________________\n\nSample Date Format :\n23 Feb 1983");
  eval("document." + formName + "." + textName + ".focus()");
  eval("document." + formName + "." + textName + ".select()");
 }
}
</script>
</head>
<body bgcolor="#FFFFFF">
<center><span style="font-family:verdana,arial,helvetica; font-weight:bold; font-size:15pt; color:#FF9900; filter:DropShadow(color=#110000, offX=1, offY=1, positive=1); width:100%">Date Format Validator</span></center>
<br>
<table width="300" align="center"><tr><td>
<font face="verdana,arial,helvetica" size="-1">
This script validates the date that the user inputs for the following sample format:
<br><font color="#808080">23 Feb 1983</font>
<br><br>Try it yourself !
<form name="form1" method="" action="">
<fieldset style="width:200px; border:#808080 solid 1px">
<legend align="left" width="330"><font face="verdana,arial,helvetica" size="-2" color="#808080">&nbsp;Date Format Validator&nbsp;</font></legend>
<table><tr><td></td></tr></table>
<center>
<input type="text" maxlength="11" style="border:#000000 solid 1px; width:120px" name="date">
<input type="button" value="Submit" style="border:#000000 outset 1px; background:#EFEFEF; height:20px; cursor:pointer" onClick="javascript:validate_date("form1","date");">
<br><font size="-2" color="#808080">(Click on the button to submit)</font>
</center>
<table><tr><td></td></tr></table>
</fieldset>
</form>
<hr style="color:#FF9900; height:1px">
&#169; 2002 <a href="http://www.qiksearch.ru" title="Click here to visit Qiksearch.ru"><font color="#808080">Premshree Pillai</font></a>.
</font>
</td></tr></table>
</body>
</html>



Date Validation in a Form

/*
JavaScript Bible, Fourth Edition
by Danny Goodman 
John Wiley & Sons CopyRight 2001
*/

<HTML>
<HEAD>
<TITLE>Date Entry Validation</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
// **BEGIN GENERIC VALIDATION FUNCTIONS**
// general purpose function to see if an input value has been entered at all
function isEmpty(inputStr) {
    if (inputStr == "" || inputStr == null) {
        return true
    }
    return false
}
// function to determine if value is in acceptable range for this application
function inRange(inputStr, lo, hi) {
    var num = parseInt(inputStr, 10)
    if (num < lo || num > hi) {
        return false
    }
    return true
}
// **END GENERIC VALIDATION FUNCTIONS**
function validateMonth(field, bypassUpdate) {
    var input = field.value
    if (isEmpty(input)) {
        alert("Be sure to enter a month value.")
        select(field)
        return false
    } else {
        input = parseInt(field.value, 10)
        if (isNaN(input)) {
            alert("Entries must be numbers only.")
            select(field)
            return false
        } else {
            if (!inRange(input,1,12)) {
                alert("Enter a number between 1 (January) and 12 (December).")
                select(field)
                return false
            }
        }
    }
    if (!bypassUpdate) {
        calcDate()
    }
    return true
}
function validateDate(field) {
    var input = field.value
    if (isEmpty(input)) {
        alert("Be sure to enter a date value.")
        select(field)
        return false
    } else {
        input = parseInt(field.value, 10)
        if (isNaN(input)) {
            alert("Entries must be numbers only.")
            select(field)
            return false
        } else {
            var monthField = document.birthdate.month
            if (!validateMonth(monthField, true)) return false
            var monthVal = parseInt(monthField.value, 10)
            var monthMax = new Array(31,31,29,31,30,31,30,31,31,30,31,30,31)
            var top = monthMax[monthVal]
            if (!inRange(input,1,top)) {
                alert("Enter a number between 1 and " + top + ".")
                select(field)
                return false
            }
        }
    }
    calcDate()
    return true
}
function validateYear(field) {
    var input = field.value
    if (isEmpty(input)) {
        alert("Be sure to enter a year value.")
        select(field)
        return false
    } else {
        input = parseInt(field.value, 10)
        if (isNaN(input)) {
            alert("Entries must be numbers only.")
            select(field)
            return false
        } else {
            if (!inRange(input,1900,2005)) {
                alert("Enter a number between 1900 and 2005.")
                select(field)
                return false
            }
        }
    }
    calcDate()
    return true
}
function select(field) {
    field.focus()
    field.select()
}
function calcDate() {
    var mm = parseInt(document.birthdate.month.value, 10)
    var dd = parseInt(document.birthdate.date.value, 10)
    var yy = parseInt(document.birthdate.year.value, 10)
    document.birthdate.fullDate.value = mm + "/" + dd + "/" + yy
}
function checkForm(form) {
    if (validateMonth(form.month)) {
        if (validateDate(form.date)) {
            if (validateYear(form.year)) {
                return true
            }
        }
    }
    return false
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="birthdate" ACTION="mailto:info@wbex.ru" METHOD=POST onSubmit="return checkForm(this)">
Please enter your birthdate...<BR>
Month:<INPUT TYPE="text" NAME="month" VALUE=1 SIZE=2 onChange="validateMonth(this)">
Date:<INPUT TYPE="text" NAME="date" VALUE=1 SIZE=2 onChange="validateDate(this)">
Year:<INPUT TYPE="text" NAME="year" VALUE=1900 SIZE=4 onChange="validateYear(this)">
<P>
Thank you for entering:<INPUT TYPE="text" NAME="fullDate" SIZE=10><P>
<INPUT TYPE="submit"> <INPUT TYPE="Reset">
</FORM>
</BODY>
</HTML>