Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Popup before a certain date 1

Status
Not open for further replies.

damipera

Technical User
Dec 1, 2005
134
GB

hi guys, i have below a script that checks if there's a date and if it's in the format i need. and this works. but what i'd like to do is to also check if the entered date is before a certain date (e.g. 01-jan-2010). if the date entered is before that date, then a poup should show. any idea how i could do it?

thanks

Code:
<script type="text/javascript" language="javascript">
function formCheck(){ 
	var theForm = document.forms['form1'];
	var dte_textfieldRE = /^(0[1-9]|[12][0-9]|3[01])\-((jan)|(feb)|(mar)|(apr)|(may)|(jun)|(jul)|(aug)|(sep)|(oct)|(nov)|(dec))\-(19|20)\d\d$/i;
	var errMsg = "";
	var setfocus = "";

	if (!dte_textfieldRE.test(theForm['textfield'].value)){
		errMsg = "Date is required";
		setfocus = "['textfield']";
	}
	if (errMsg != ""){
		alert(errMsg);
		eval("theForm" + setfocus + ".focus()");
	}
	else theForm.submit();
}
</script>
</head>

<body>
<form action="" method="post" name="form1" id="form1" onsubmit="formCheck();return false;">
  <input type="text" name="textfield" />
  <input type="submit" name="Submit" value="Submit" />
</form>

 
Will Date(theForm['textfield'].value))-Date(2010,1,1) not work for you?

_________________
Bob Rashkin
 
Hi

Bob said:
Will Date(theForm['textfield'].value))-Date(2010,1,1) not work for you?
I would say, probably not :
Code:
[blue]>>> Date('01-jan-2010')[/blue]
[red]"Wed Jun 20 2012 16:54:15 GMT+0300 (EEST)"[/red]

Feherke.
[link feherke.github.com/][/url]
 
hi feherke, how do i tackle this then?

any suggestions? thanks
 
Hi

[ul]
[li]Clean up your regular expression. There are lots of pointless capture groups.[/li]
[li]Use [tt]exec()[/tt] instead of [tt]test()[/tt] to get the captured substrings instead of a single boolean value.[/li]
[li]Capture the entire year.[/li]
[li]Concatenate the pieces in year-month-day order. ( No, do not add the literal dash ( - ). )[/li]
[li]Instantiate a [tt]Date[/tt] object from the resulted string.[/li]
[/ul]
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]formCheck[/color][teal]()[/teal] [teal]{[/teal] 
	[b]var[/b] theForm [teal]=[/teal] document[teal].[/teal]forms[teal][[/teal][green][i]'form1'[/i][/green][teal]];[/teal]
	[b]var[/b] dte_textfieldRE [teal]=[/teal] [fuchsia]/^(0[1-9]|[12][0-9]|3[01])\-(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\-((?:19|20)\d\d)$/i[/fuchsia][teal];[/teal]
	[b]var[/b] errMsg [teal]=[/teal] [green][i]""[/i][/green][teal];[/teal]
	[b]var[/b] setfocus [teal]=[/teal] [green][i]""[/i][/green][teal];[/teal]
	[b]var[/b] part[teal]=[/teal]dte_textfieldRE[teal].[/teal][COLOR=darkgoldenrod]exec[/color][teal]([/teal]theForm[teal].[/teal]elements[teal][[/teal][green][i]'textfield'[/i][/green][teal]].[/teal]value[teal]);[/teal]

	[b]if[/b] [teal](![/teal]part[teal])[/teal] [teal]{[/teal]
		errMsg [teal]=[/teal] [green][i]"Date is required"[/i][/green][teal];[/teal]
		setfocus [teal]=[/teal] [green][i]'textfield'[/i][/green][teal];[/teal]
	[teal]}[/teal] [b]else[/b] [b]if[/b] [teal]([/teal][b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]([/teal]part[teal][[/teal][purple]3[/purple][teal]]+[/teal][green][i]' '[/i][/green][teal]+[/teal]part[teal][[/teal][purple]2[/purple][teal]]+[/teal][green][i]' '[/i][/green][teal]+[/teal]part[teal][[/teal][purple]1[/purple][teal]])<[/teal][b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]([/teal][purple]2010[/purple][teal],[/teal][purple]0[/purple][teal],[/teal][purple]1[/purple][teal]))[/teal] [teal]{[/teal]
		errMsg [teal]=[/teal] [green][i]"Date is before 01-jan-2010"[/i][/green][teal];[/teal]
		setfocus [teal]=[/teal] [green][i]'textfield'[/i][/green][teal];[/teal]
	[teal]}[/teal]

	[b]if[/b] [teal]([/teal]errMsg [teal]!=[/teal] [green][i]""[/i][/green][teal])[/teal] [teal]{[/teal]
		[COLOR=darkgoldenrod]alert[/color][teal]([/teal]errMsg[teal]);[/teal]
		theForm[teal].[/teal]elements[teal][[/teal]setfocus[teal]].[/teal][COLOR=darkgoldenrod]focus[/color][teal]();[/teal]
	[teal]}[/teal]
	[b]else[/b] theForm[teal].[/teal][COLOR=darkgoldenrod]submit[/color][teal]();[/teal]
}
Additional notes :[ul]
[li]January is month 0. 1 means February.[/li]
[li]Using [tt]eval()[/tt] is a bad habit. Avoid it.[/li]
[li]The official way to access [tt]form[/tt] elements dynamically is through the [tt]elements[/tt] collection. Not sure if direct subscript is a HTML5 improvement or what, but I would stick with the [tt]elements[/tt] collection for now.[/li]
[/ul]

Feherke.
[link feherke.github.com/][/url]
 
Wow! thanks very much Feherke. That is AWESOME!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top