When we have some controls in ASP.NET, it’s of course preferable to use both clientside and serverside validation. Clientside for usability purposes, and serverside for security purposes.
However, this easily gets quite ugly. Of course, if your validation is a length check or a regular expression, it is easy to maintain. However, let’s say that your validation is more advanced – what to do?
One way is to implement the exact same functionality in both server code and Javascript. Another way is that both calls a webservice with the same logic, but then your AJAX call has to be synchronous which in general is bad practice.
So, my question is, what’s the best way to perform both serverside and clientside validation?
Your ajax call does not have to be synchronous for validation; it just needs to be quick:
Another option (besides @user133811’s of using JS is both places) is to find a cross-compiler, which will compile your language of choice into JavaScript. These may or may not be available, however.
I’ve generally found that doing complex validation on the client is unnecessary and undesirable. Yes, let me know a field is required, or if a username exists (via ajax), or simple things, but if this is a complex mortgage application or something, I expect I’ll have to submit before finding out I need to include my W2 if I declare my pet goldfish as co-signatory (or whatever, you get the point).