|
про клиента вопрос
|
|||
---|---|---|---|
#18+
Как на стороне клиента, проверить валидность некой даты, собранной из трех частей (d.m.y). Желательно в JavaScript Спасибо. ... |
|||
:
Нравится:
Не нравится:
|
|||
17.08.2004, 21:31 |
|
про клиента вопрос
|
|||
---|---|---|---|
#18+
<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> ... |
|||
:
Нравится:
Не нравится:
|
|||
17.08.2004, 22:10 |
|
про клиента вопрос
|
|||
---|---|---|---|
#18+
<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> ... |
|||
:
Нравится:
Не нравится:
|
|||
17.08.2004, 22:12 |
|
про клиента вопрос
|
|||
---|---|---|---|
#18+
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; ... |
|||
:
Нравится:
Не нравится:
|
|||
17.08.2004, 22:14 |
|
про клиента вопрос
|
|||
---|---|---|---|
#18+
http://members.ozemail.com.au/~dcrombie/javascript/index.html ... |
|||
:
Нравится:
Не нравится:
|
|||
17.08.2004, 22:14 |
|
про клиента вопрос
|
|||
---|---|---|---|
#18+
http://www.digitalroom.net/index2.html ... |
|||
:
Нравится:
Не нравится:
|
|||
17.08.2004, 22:15 |
|
|
start [/forum/topic.php?fid=23&fpage=504&tid=1479467]: |
0ms |
get settings: |
11ms |
get forum list: |
14ms |
check forum access: |
4ms |
check topic access: |
4ms |
track hit: |
38ms |
get topic data: |
13ms |
get forum data: |
3ms |
get page messages: |
45ms |
get tp. blocked users: |
2ms |
others: | 298ms |
total: | 432ms |
0 / 0 |