powered by simpleCommunicator - 2.0.51     © 2025 Programmizd 02
Форумы / PHP, Perl, Python [игнор отключен] [закрыт для гостей] / про клиента вопрос
7 сообщений из 7, страница 1 из 1
про клиента вопрос
    #32653700
хочу
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Как на стороне клиента, проверить валидность некой даты, собранной из трех частей (d.m.y).
Желательно в JavaScript

Спасибо.
...
Рейтинг: 0 / 0
про клиента вопрос
    #32653721
<script language="JavaScript"><!--
function y2k(number) { return (number<1000) ? number+1900 : number; }
function isDate (day,month,year) {
// допустимы следующие форматы:
// isDate(dd,mm,ccyy), или
// isDate(dd,mm) - текущий год по умолчанию, или
// isDate(dd) - месяц и год берется текущий.
// Месяц должен быть от 1 до 12, также
// год в формате ccyy т.е. векгод.
var today = new Date();
year = ((!year) ? y2k(today.getYear()):year);
month = ((!month) ? today.getMonth():month-1);
if (!day) return false
var test = new Date(year,month,day);
if ( (y2k(test.getYear()) == year) &&
(month == test.getMonth()) && (day == test.getDate()) )
return true;
else return false
}

if (isDate(31,2,1997)) document.write("Valid");
else document.write("Invalid");
//--></script>
...
Рейтинг: 0 / 0
про клиента вопрос
    #32653722
Могу
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
<html>
<head>
<!---->

<script language=javascript>
/*Function name :isDigit(theDigit) */
/*Usage of this function :test for an digit */
/*Input parameter required:thedata=string for test whether is digit */
/*Return value :if is digit,return true */
/* else return false */
function isDigit(theDigit)
{
var digitArray = new Array('0','1','2','3','4','5','6','7','8','9'),j;

for (j = 0; j < digitArray.length; j++)
{if (theDigit == digitArray[j])
return true
}
return false

}
/*************************************************************/*Function name :isPositiveInteger(theString) */
/*Usage of this function :test for an +ve integer */
/*Input parameter required:thedata=string for test whether is +ve integer*/
/*Return value :if is +ve integer,return true */
/* else return false */
/*function require :isDigit */
/*************************************************************function isPositiveInteger(theString)
{
var theData = new String(theString)

if (!isDigit(theData.charAt(0)))
if (!(theData.charAt(0)== '+'))
return false

for (var i = 1; i < theData.length; i++)
if (!isDigit(theData.charAt(i)))
return false
return true
}
/**********************************************************************/
/*Function name :isDate(s,f) */
/*Usage of this function :To check s is a valid format */
/*Input parameter required:s=input string */
/* f=input string format */
/* =1,in mm/dd/yyyy format */
/* else in dd/mm/yyyy */
/*Return value :if is a valid date return 1 */
/* else return 0 */
/*Function required :isPositiveInteger() */
/**********************************************************************/
function isDate(s,f)
{var a1=s.split("/");
var a2=s.split("-");
var e=true;
if ((a1.length!=3) && (a2.length!=3))
{
e=false;
}
else
{if (a1.length==3)
var na=a1;
if (a2.length==3)
var na=a2;
if (isPositiveInteger(na[0]) && isPositiveInteger(na[1]) && isPositiveInteger(na[2]))
{ if (f==1)
{var d=na[1],m=na[0];
}
else
{var d=na[0],m=na[1];
}
var y=na[2];
if (((e) && (y<1000)||y.length>4))
e=false
if (e)
{
v=new Date(m+"/"+d+"/"+y);
if (v.getMonth()!=m-1)
e=false;
}
}
else
{
e=false;
}
}
return e
}
function checkDate(v)
{
var s=v.a.value;
if (isDate(s,0)) //dd/mm/yyyy format
alert("The inputted date value is valid!");
else
alert("The inputted date value is not valid!");
return false;
}
</script>

</head>
<body>

<dir>
Validate the following date format:
dd/mm/yyyy
dd-mm-yyyy
mm/dd/yyyy
mm-dd-yyyy
</dir>
<BR>
<form action="#" onsubmit="return checkDate(this)">
Date value:<input type="text" name="a"><input type="submit" value="Check it!">
</form>

<BR><center><a href='http://www.js-x.com'>JS-X.com</a></center>
</body>
</html>
...
Рейтинг: 0 / 0
про клиента вопрос
    #32653724
Хачу
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
function isDate(dateStr) {

//Does a regex to pull out the MM DD and YYYY without the /.
var datePat = /^(\d{2})(\/)(\d{2})(\/)(\d{4})$/;

//Creates the array so we can validate the regex.
var matchArray = dateStr.match(datePat);

//Validates the result of the regex.
if (matchArray == null) {
alert("You must enter a date.\nPlease enter the date in the following format: DD/MM/YYYY\nExample: 12/30/2003");
return false;
}

//Set the month using the regex array.
enteredMonth = matchArray[1]; // parse date into variables

//Set the day using the regex array.
enteredDay = matchArray[3];

//Set the year using the regex array.
enteredYear = matchArray[5];
return true;
}
//////////////////////////////////////////////////////////////

Here is the validation function:

//////////////////////////////////////////////////////////////
function validateForm(theForm)
{
if (isDate(theForm.elements[enteredDate.value)==false){
return false;
}
return true;
}
//////////////////////////////////////////////////////////////

I've tried doing something like this:

var maxDate = 90;
var date1 = new Date(NMonth,NDay,NYear);
var date2 = new Date(enteredMonth,enteredDay,enteredYear);
var milliseconds1 = date1.getTime();
var milliseconds2 = date2.getTime();
var difference = milliseconds2 - milliseconds1;
var daysDifference = Math.floor(difference/1000/60/60/24);
difference = difference - daysDifference*1000*60*60*24;

if (difference > maxDate)
{
alert("date1= " + date1 + "|||date2= " + date2 + "|||difference=" + difference + "|||daysDifference=" + daysDifference + "\nThe year (YYYY) you have entered is not valid.\nPlease enter a valid year on or before " + enteredYear + ".");
return false;
}
return true;
...
Рейтинг: 0 / 0
про клиента вопрос
    #32653725
ЯЗХЩШ
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
http://members.ozemail.com.au/~dcrombie/javascript/index.html
...
Рейтинг: 0 / 0
про клиента вопрос
    #32653726
ДДЛЛЛ
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
http://www.digitalroom.net/index2.html
...
Рейтинг: 0 / 0
про клиента вопрос
    #32653727
ЪЪЪЪЪ
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
http://www.jsmadeeasy.com/javascripts/Forms/Validation%20(Credit%20Card)/
...
Рейтинг: 0 / 0
7 сообщений из 7, страница 1 из 1
Форумы / PHP, Perl, Python [игнор отключен] [закрыт для гостей] / про клиента вопрос
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]