I am using VS2008 and I trying to use the Range Validator for the following:
I have a text box that a user will input a date into for an appointment - the date entered must be at least 7 days after the request has been made (current date) but less than 90 days into the future.
I have set my range validator to Type = Date and Control to validate = Textbox11
I am now lost where the rest of the code goes to achieve the above?
15 3743
A little bit googling would have solved your problem.
Here goes your code : - <html>
-
<body>
-
-
<form runat="server">
-
Enter a date between 2009-12-23 and 2010-03-15:
-
<br />
-
<asp:TextBox id="tbox1" runat="server" />
-
<br /><br />
-
<asp:Button Text="Submit" runat="server" />
-
<br /><br />
-
<asp:RangeValidator
-
ControlToValidate="tbox1"
-
MinimumValue="2009-12-23"
-
MaximumValue="2010-03-15"
-
Type="Date"
-
EnableClientScript="false"
-
Text="The date must be between 2009-12-23 and 2010-03-15!"
-
runat="server" />
-
</form>
-
-
</body>
-
</html>
-
For further reading you can go to => http://www.w3schools.com/aspnet/cont...evalidator.asp
Thanks for that Sanjib :)
I didn't know that there was an ASP Classic version of the RangeValidator control!
Since the original poster (chicane) is having problems with the .NET RangeValidator control I figured I'd just add a link to a resource for that control: RangeValidator Control (General Reference)
-Frinny
Hi there
Thanks for your response. I don't think I was clear in my post or I haven't explianed it very well.
You mentioned in your response that the Minimum Value will be 2009/12/23 but this wouldn't be the case if someone submitted the request on a different date, how is the code set so that it works off the Current date that the form is submitted, so that it can be calculated off any date?
Thanks again for your help.
I would probably just write a JavaScript function that checks the dates.
(Remember that client side validation is easy to get around so you should always do server side validation too).
Call the JavaScript function when you submit the page and have it check if the values are valid. This JavaScript function would use the JavaScript Date Object to make sure that the "to-date" was greater than the "from-date"....if it isn't then the function would return false to cancel the page submit (and maybe display an alert() stating that the page is invalid...or otherwise let the user know that the content is not valid) How to use JavaScript in ASP.NET
-Frinny
Hi
Thanks for getting back to me, I will give it a try shortly and let you know how I get on.
thanks
Javascript? Why are you coding in .Net?
I realize it is too late for the original poster, but put this code in your PageLoad() event : -
RangeValidator1.MaximumValue = DateTime.Now.AddDays(90).ToShortDateString();
-
RangeValidator1.MinimumValue = DateTime.Now.AddDays(-5).ToShortDateString();
-
This will cause the validator to accept dates between 5 days ago and 90 days in the future.
YerKiddinMe,
You're quite right, you should always use server side validation; however, if you want to prevent an unnecessary round trip to the server you can also do client side validation...by this I mean that you can validate something client-side using JavaScript and if it detects that the page is invalid then the JavaScript prevents the page from submitting...displaying error messages prompting the user to correct the validation errors.
It is easy to get around client side validation so you should always do server side validation as well.
VS 2008 validators, by default, run on client side. VS writes the javascript for you.
:) exactly why it's so easy to prevent unnecessary round trips to the server :)
However, sometimes you want to customize these and you'll want to write your own JavaScript functions (as is being done in this case....I think...I hope...?)
Personally, I never write JavaScript if I don't have to. In this case, two lines of .Net code behind covers the problem of validating a date range based on the current date completely.
Don't sell the built in Validators short, they are quite powerful. The only case where I can see value in writing JavaScript for client side validation in a .Net web application would be if the required range changed based on the values entered in other fields.
If, say, I had a form where I had a drop down list of events and the acceptable date range varied depending on the selected event, JavaScript might be a justifiable choice. But being lazy, I'd probably use the AjaxToolkit to minimize the time cost of the postback, and do the validation server side...more JavaScript that VS will write for me. ;)
Ajax in .NET (I'm talking UpdatePanels here) doesn't eliminate any time cost.
It takes the same amount of time as a full page postback and the page undergoes the full ASP.NET life-cycle server side. The only time it saves is rendering in the browser (which is insignificant most of the time).
I agree that it's a lot smoother than a full page postback....but there's no time or resources saved...whereas JavaScript (the validators) do save you the whole process.
I used to be like you.
I hated getting my hands all dirty with JavaScript...and truth be told I only add it now once I know everything works well on the server and if I had the time.
The more you work in the web environment the more you can appreciate what JavaScript can do for you and how it can make your applications actually more efficient :)
-Frinny
It's not a question of "getting my hands dirty with JavaScript", it's a question of efficiency. I won't write JavaScript unless .Net can't do what I need. In the case of the original problem on this thread, VS Validators write client side JavaScript for me, so it is a waste of time to write that JavaScript myself. In the more complex scenario I described, the advantage of hand rolled JavaScript will not be significant unless I have a huge page, slow servers, or thousands of concurrent users.
I've been writing web applications in an enterprise environment for over 4 years, and have found the need to write my own JavaScript maybe 3 or 4 times, and functionality was the reason rather than expensive postbacks. The question becomes "How much more efficient will the application be, and is it worth the loss of efficiancy in coding time?" If I spend even 15 extra minutes making something a half second faster, will my boss think I've spent my time well?
@YerKiddinMe
It depends on how scalable your application needs to be. I like the server side code too, because it's much easier to me, but when you have tens of thousands of users, that "half-second" starts to add up, but not just in time saving, but in bandwidth/server resource utilization as well. Why make unnecessary postbacks? If you can prevalidate your stuff using a JS method, that's several postbacks saved, which when scaled to lots of users, is a considerable amount.
I think it's best to use the best tool for the job. When it comes to prevalidating your inputs, Javascript is significantly more efficient than trips back to the server.
"When it comes to prevalidating your inputs, Javascript is significantly more efficient than trips back to the server."
I agreed to that several posts ago. It is also much more efficient to use the built-in .Net Validators, which, by default, write client side Javascript, thus still saving the trip to the server, while also saving me from having to write the JavaScript myself. That was my original point, and I apologize for getting sidetracked with trivial details not germane to the original post.
[...he said before continuing down the side track...]
Hand coded JavaScript is a good choice for validation code ONLY in rare circumstances. I'm not arguing against client side validation, I'm arguing against custom writing it yourself when .Net will do it for you. When your validarion requirements are so complex that .Net validation will require a postback, there are many factors to consider before choosing either route.
As you mentioned, how scalable does it need to be is one question, but the size of the page makes a big difference, too. A small form with five or ten input fields is a lot less load than the form I am working on now with nearly 250 controls on it, and also less likely to fail validation.
I'm not saying JavaScript is never the best choice. First that would just be wrong, and second I already mentioned above, I DO use it when I NEED it. But to say home-rolled JavaScript is always the best choice is also wrong.
But to say home-rolled JavaScript is always the best choice is also wrong.
True, though I don't really think anyone was saying that. The best thing to do is learn the tools well enough to know when it's appropriate to use which tool is best for the job.
Anyway, I think we've dragged this post off topic enough.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: sundarvenkata |
last post by:
Is multi-targeting possible using Visual Studio Express? If not, do
I have any other options to target a 1.1 framework using Visual Studio
Express? Thanks for your help, Sundar
|
by: Jon Skeet [C# MVP] |
last post by:
Just in case people had missed it, Visual Studio 2008 is now available
for download for MSDN subscribers, and the Express editions are also
out:
http://www.microsoft.com/express/download/
--...
|
by: JJ |
last post by:
I see the new software is 'RTM' but what does that mean in terms of when we
can actually purchase it?
Thanks,
JJ
|
by: shapper |
last post by:
Hello,
Does anyone knows what is the difference between Microsoft Visual
Studio 2008 Team Foundation Server and Microsoft Visual Studio 2008
Professional?
Thanks,
Miguel
|
by: Colin J. Williams |
last post by:
The readme.txt has:
All you need to do is open the workspace
"pcbuild.sln" in Visual Studio,
select the desired combination of
configuration and platform and eventually
build the solution....
|
by: =?Utf-8?B?RHIuIFMu?= |
last post by:
What is visual web developer 2008? Is this used to construct websites as is
Expression?
Thanks in advance.
|
by: Cramer |
last post by:
So, what is the relationship between Visual Studio and Visual Web Developer.
I find a lot of documentation on MSDN that presents Visual Web Developer as
it's own stand-alone product (which I'd...
|
by: Cirene |
last post by:
From my experience/undertanding SQL Server (and SQL Express) is the
preferred db to use with Visual Studio and is very nicely integrated with
it. It works well with the built in membership, etc......
|
by: Nicopilami |
last post by:
Hi, i'm using Visual Studio WebDevelopper 2008 express all the day,
and i would like to install some add in i've found, to write and
manage javascript scripts more easily :...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |