PDA

View Full Version : Newbie needs help in javascript! validation


Vlince
01-17-2000, 02:24 PM
Hello! I've looked around in many web site to finally get what i wanted. I was looking for a function in javascript to validate the Date. this is the piece of code I got:<BR><BR><SCRIPT><BR>function checkdate()<BR>{<BR><BR> var err=0<BR> var psj=0;<BR> <BR> a=document.frmAffiche.txtDateRecue.value<BR> if (a.length > 8) err=1<BR> b = a.substring(0, 2)// month<BR> <BR> c = a.substring(2, 3)// '/'<BR> <BR> d = a.substring(3, 5)// day<BR> <BR> e = a.substring(5, 6)// '/'<BR> <BR> f = a.substring(6, 8)// year<BR><BR> //basic error checking<BR> if (b<1 || b>12) err = 1<BR> if (c != '/') err = 1<BR> if (d<01 || d>31) err = 1<BR> if (e != '/') err = 1<BR> if (f<0 || f>99) err = 1<BR> <BR> //advanced error checking<BR><BR> // months with 30 days<BR> if (b==4 || b==6 || b==9 || b==11)<BR> {<BR> if (d==31) err=1<BR> }<BR><BR> // february, leap year<BR> if (b==2)<BR> {<BR> // feb<BR> var g=parseInt(f/4)<BR> if (isNaN(g)) {<BR> err=1<BR> }<BR><BR> if (d>29) err=1<BR> if (d==29 && ((f/4)!=parseInt(f/4))) err=1<BR> }<BR><BR> if (err==1){<BR> alert('Wrong input!');<BR> return (false)<BR> }<BR> else{<BR> alert('OK!');<BR> return (true)<BR> }<BR><BR>}<BR><BR></SCRIPT><BR><BR><!--This is my form--><BR><FORM method="post" action="yoyo.asp" id=frmAffiche name=frmAffiche onSubmit="return checkdate()"><BR><BR><INPUT id=txtDateRecue name=txtDateRecue value=<%=Date()%>><BR><INPUT id=button1 name=button1 type=submit value=Validate> <BR></FORM><BR><BR>When this page is executed, in my textbox i get the Date like this: 1/17/00<BR>and if i press the Validate button I get the alert box!<BR>The reason is because the date should be: 01/17/00<BR><BR>Now my question is: I would like the user to enter 1/17/00 and then click the Validate button without having any problems! and I suppose if we where the: 12/8/00 I would also get the alert box! so how can I change or what can I change in the function that would allow me to write: 12/8/00 or 1/17/00 or 2/2/00 without having the alert box popup every time!<BR>thanks<BR>Sincerely<BR>vlince

Desiree
01-19-2000, 02:30 PM
<SCRIPT language="JavaScript1.2"><BR><BR>function checkdate()<BR>{<BR><BR>var err=0<BR>var month=0<BR>var day=0<BR>var year=0<BR>var tmp<BR><BR>a=document.frmAffiche.txtDateRecue.value<BR>re = /^d{1,2}/d{1,2}/d{2}$/<BR>if (re.test(a)) {<BR> month = a.substring(0,a.indexOf ("/"))<BR> tmp = a.substring(a.indexOf ("/") + 1,a.length)<BR> day = tmp.substring(0,tmp.indexOf("/"))<BR> year = tmp.substring(tmp.indexOf("/") + 1, tmp.length)<BR> re2 = /^0d{1}$/<BR> if (re2.test(day)) day = day.substring(1,day.length)<BR> if (re2.test(month)) month = month.substring(1,month.length)<BR><BR>//basic error checking<BR> if (month<1 || month>12) err = 1<BR> if (day<1 || day>31) err = 1<BR> if (year<0 || year>99) err = 1<BR><BR>//advanced error checking<BR> if ((month==4 || month==6 || month==9 || month==11) && day == 31) err=1<BR> if ((month==2) && ((year/4)!=parseInt(year/4)) && (day>=29)) err=1<BR>}<BR><BR>if (err==1){<BR> alert('Invalid Date!');<BR> return (false)<BR>}<BR>else{ return (true) }<BR><BR>}<BR><BR></SCRIPT><BR><BR><!--This is my form--><BR><FORM method="post" action="yoyo.asp" id=frmAffiche name=frmAffiche onSubmit="return checkdate()"><BR><BR><INPUT id=txtDateRecue name=txtDateRecue value=""><BR><INPUT id=button1 name=button1 type=submit value=Validate> <BR></FORM><BR><BR>Hope this helps...Cheers!