views:

1730

answers:

3

I have a jQuery datepicker that I want to restrict non work days - weekends, public holidays etc. I have seen examples on how to do this from the client side (javascript), but is there a way to restrict the dates from server side ASP.NET code?

I figure you could do using Page.RegisterClientScriptBlock, but was wondering is there a neater way?

A: 

You just do it when you validate the data being posted, using the same logic you use client-side. You should always be validating data at server-side, NOTHING from the client side can EVER be trusted, even if you have "validation code" there and think you're requiring javascript to be on for it to work.

Doing validation client-side should be a secondary thing, just to provide a nice user experience. There is no security in any client-side code. (Go install firebug and/or the "tamper data" extensions for firefox if you don't believe me).

gregmac
Yeah I do validate server side before saving data, but I don't want the user to be able to actually select some dates too - more of a UX thing than anything else.
KiwiBastard
A: 

There only two way to restrict the dates:

  • Client Side using Javascript
    This could be done with the javascript being generated or not in the server side, but you will end up always with javascript

  • Server side
    You must compare the dates inside the webcontrols or input using your favorite.net language (c# or vb.net)

For a better UX experience, you should do the restrict client side, but if you want to be sure the data is valid, you must check it server side.

Eduardo Molteni
+2  A: 

The JQuery stuff is all client side, so there is no server side to speak of. My recommendation would be to create some thin server-side wrappers that automagically do the equivalent of writing RegisterClientScriptBlock. That way you only have to fiddle around with the Javascript once, and it always just works.

Travis