ASP.NET:同步客户端和服务器端验证规则

时间:2021-10-20 15:12:06

Are there any easy, smart ways to keep your client and server-side validation-rules synchronized?

有没有简单,智能的方法来保持客户端和服务器端验证规则的同步?

On the client side we have JavaScript, maybe some kind of framework like jQuery or YUI.

在客户端,我们有JavaScript,可能是某种框架,如jQuery或YUI。

On the server-side we have ASP.NET WebForms or ASP.NET MVC.

在服务器端,我们有ASP.NET WebForms或ASP.NET MVC。

What is validated are things like:

验证的是:

  • Correct e-mail-addresses
  • 正确的电子邮件地址
  • Correct home-addresses and postal codes
  • 正确的家庭住址和邮政编码
  • Correct credit-card numbers
  • 正确的信用卡号码

And so on.

等等。

6 个解决方案

#1


2  

You can try using Enterprise Library Validation with ASP.NET integration.

您可以尝试使用ASP.NET集成的Enterprise Library Validation。

#2


2  

<asp:RegularExpressionValidator ...> (and the other asp.net validators) implement client side javascript and server side checking to the same rules.

(和其他asp.net验证器)实现客户端javascript和服务器端检查相同的规则。 :regularexpressionvalidator>

#3


1  

write a large, common corpus of test data that embodies the validation rules, and unit test your validators against this common data.

编写一个包含验证规则的大型常见测试数据语料库,并针对此常见数据对您的验证器进行单元测试。

When your rules change, you reflect this by updating the test data and testing until everything goes green again.

当您的规则发生变化时,您可以通过更新测试数据并进行测试来反映这一点

#4


1  

I have always used the built in validators. For example if you use a RegularExpressionValidator and supply a ValidationExpression it will validate on client side (if available) and server side using the same code.

我一直使用内置的验证器。例如,如果您使用RegularExpressionValidator并提供ValidationExpression,它将在客户端(如果可用)和服务器端使用相同的代码进行验证。

You can write your own custom validators by deriving from BaseValidatior. Doing this allows you to create Server Valdiation by overriding EvaluteIsValid. You can then add client validation later if it is necessary.

您可以通过从BaseValidatior派生来编写自己的自定义验证器。这样做允许您通过重写EvaluteIsValid来创建服务器Valdiation。如果有必要,您可以稍后添加客户端验证。

#5


1  

This is not a real-world solution, but check out the Axial project on CodePlex. It is a project that converts C# to Javascript for the web, and has a control that lets you use the same code for server side validation and client side validation. It's not ready for production, but I'm curious to see if this is what you're looking for.

这不是一个真实的解决方案,但请查看CodePlex上的Axial项目。它是一个将C#转换为Web的Javascript的项目,并且具有一个控件,允许您使用相同的代码进行服务器端验证和客户端验证。它尚未准备好投入生产,但我很想知道这是否是您正在寻找的。

#6


1  

xVAL is quite a bit easier than the Enterprise Library Validation and handles model bound validation for both Client and Server.

xVAL比企业库验证更容易,并处理客户端和服务器的模型绑定验证。

#1


2  

You can try using Enterprise Library Validation with ASP.NET integration.

您可以尝试使用ASP.NET集成的Enterprise Library Validation。

#2


2  

<asp:RegularExpressionValidator ...> (and the other asp.net validators) implement client side javascript and server side checking to the same rules.

(和其他asp.net验证器)实现客户端javascript和服务器端检查相同的规则。 :regularexpressionvalidator>

#3


1  

write a large, common corpus of test data that embodies the validation rules, and unit test your validators against this common data.

编写一个包含验证规则的大型常见测试数据语料库,并针对此常见数据对您的验证器进行单元测试。

When your rules change, you reflect this by updating the test data and testing until everything goes green again.

当您的规则发生变化时,您可以通过更新测试数据并进行测试来反映这一点

#4


1  

I have always used the built in validators. For example if you use a RegularExpressionValidator and supply a ValidationExpression it will validate on client side (if available) and server side using the same code.

我一直使用内置的验证器。例如,如果您使用RegularExpressionValidator并提供ValidationExpression,它将在客户端(如果可用)和服务器端使用相同的代码进行验证。

You can write your own custom validators by deriving from BaseValidatior. Doing this allows you to create Server Valdiation by overriding EvaluteIsValid. You can then add client validation later if it is necessary.

您可以通过从BaseValidatior派生来编写自己的自定义验证器。这样做允许您通过重写EvaluteIsValid来创建服务器Valdiation。如果有必要,您可以稍后添加客户端验证。

#5


1  

This is not a real-world solution, but check out the Axial project on CodePlex. It is a project that converts C# to Javascript for the web, and has a control that lets you use the same code for server side validation and client side validation. It's not ready for production, but I'm curious to see if this is what you're looking for.

这不是一个真实的解决方案,但请查看CodePlex上的Axial项目。它是一个将C#转换为Web的Javascript的项目,并且具有一个控件,允许您使用相同的代码进行服务器端验证和客户端验证。它尚未准备好投入生产,但我很想知道这是否是您正在寻找的。

#6


1  

xVAL is quite a bit easier than the Enterprise Library Validation and handles model bound validation for both Client and Server.

xVAL比企业库验证更容易,并处理客户端和服务器的模型绑定验证。