简体   繁体   English

如何一次触发客户端和服务器端验证?

[英]how to fire both client side and server side validations at once?

I have a textbox and I do CustomValidation which does client side and server side validations, how do I fire both client side and server side validations at once? 我有一个文本框,我执行CustomValidation来进行客户端和服务器端验证,如何同时启动客户端和服务器端验证? now server side validation fires only after client side validation. 现在,仅在客户端验证之后才触发服务器端验证。

is it possible? 可能吗?

Basically : No, you can rise one after the other if you need but client side validation is there to avoid having to call the server in order to validate. 基本上:不,如果需要,您可以一个接一个地升级,但是可以进行客户端验证以避免调用服务器进行验证。 If you need to call the server anyway, why do you bother writing client validation script ? 如果仍然需要调用服务器,为什么还要编写客户端验证脚本呢?

edit : 编辑:

If you want the server validation to take place before the form is posted, I suggest you use Ajax to call the server validation routine within you client side validation script. 如果希望服务器验证在表单发布之前进行,建议您使用Ajax在客户端验证脚本中调用服务器验证例程。 It requires a little trick because Ajax is asynchronous by definition while the client side validation is not. 它需要一些技巧,因为Ajax根据定义是异步的,而客户端验证不是。 Start here : http://www.codeproject.com/KB/ajax/AjaxValidation.aspx 从这里开始: http : //www.codeproject.com/KB/ajax/AjaxValidation.aspx

Client and server side validations can have different purposes and one cant always assume to stay safe with client side validations. 客户端验证和服务器端验证可以有不同的用途,并且不能总是假设保持客户端验证的安全性。

By default, once the client events are over, the request is sent out to the server and that's how the page cycle takes place. 默认情况下,一旦客户端事件结束,请求就会发送到服务器,这就是页面循环的过程。 But if you need to send a server request directly, you would need to make use of AJAX and send an async request. 但是,如果您需要直接发送服务器请求,则需要使用AJAX并发送异步请求。

But logically, It would be advisable to handle the two validations separately, for a cleaner and better approach. 但从逻辑上讲,建议分开处理这两个验证,以获取更清洁,更好的方法。

I'm guessing you have some CustomValidator that only has a server-side implementation, and it runs after your regular validators have already run. 我猜您有一些CustomValidator,它仅具有服务器端实现,并且在常规验证程序已经运行之后才能运行。 That way the user would first be presented with validation errors from the regular validators, and after fixing those, has to go through the custom validator, which is confusing. 这样,首先会向用户显示来自常规验证器的验证错误,并且在修复这些错误之后,必须经过自定义验证器,这会造成混淆。 I suggest turning off all client validation (set EnableClientValidation to false), and running the whole thing server-side only. 我建议关闭所有客户端验证(将EnableClientValidation设置为false),并仅在服务器端运行整个程序。 That way all validators run at the same time. 这样,所有验证器将同时运行。

Menno 门诺

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM