Joe Enos
Joe Enos

Reputation: 40403

ASP.NET validation using AJAX without update panels

I'm looking for a way to use custom ASP.NET validators to validate input, without using UpdatePanels, and without a full postback.

The validators do several things - not just length/regex, but some other non-standard stuff as well.

Javascript is required for our users, so I don't have to worry about normal users who have javascript turned off.

I see several options, but none are ideal:

1) Suck it up and use UpdatePanels. I'm in the "UpdatePanels are evil" group, so I'd prefer not to do this.

2) Without using validation controls, manually validate the fields by passing the values to a PageMethod static method, via jQuery or any other AJAX framework. This would require client and server coding each time I needed to use a validator.

3) Use jQuery (or any other javascript framework) validation for client validation, then if they somehow get by that, have server-code validation controls for full postback. This would require all of the validation rules to be written in javascript as well as C#. I don't care about the full failed postback at this point, because the javascript validation would catch real users who weren't trying to screw me over.

Is there alternative out there for using real CustomValidator controls, with partial postback, C# code only, for validating input without UpdatePanels and without a lot of redundant javascript?

Thanks

Upvotes: 1

Views: 592

Answers (2)

joelt
joelt

Reputation: 2680

Honestly, I think 2 is a good option. CustomValidator or not, you'll write the same server code to do the validation. You can do all the validation code in C#, and just write JS to call your validation method, which could be a PageMethod or a separate ashx. And really, you could probably write one JS function that takes parameters to make the AJAX call pretty easily.

Upvotes: 1

davidsleeps
davidsleeps

Reputation: 9503

I'd use a mixture of 2 and 3.

First- Server side validation will exist regardless...

So I would make the fields that require simple validation logic javascript based (numbers, text, string lengths, string certain formats etc) with no ajax callbacks, and then use callbacks to the server where business rules or otherwise more complex validation needs to occur, so that complex rules are only coded once.

If you had to pick between either a very fast response (or no delay...js only), or valid data, I'd expect that valid data would win...So using 2 and 3 together hopefully will give you a faster response, but without compromising valid data.

Ajax callbacks to a generic handler etc should be a bit quicker than an UpdatePanel anyway as far less data would be being transferred...

Upvotes: 1

Related Questions