简体   繁体   English

选择最小值和最大值的验证

[英]Validation for selecting a minimum and maximum

I have a form where a min and a max have to be entered by user.我有一个表单,其中用户必须输入最小值和最大值。 What is the recommendation for validation for this case.对此案例的验证建议是什么。 Only server validation or client and server validation.仅服务器验证或客户端和服务器验证。

How do you validate on client both inputs?您如何在客户端验证两个输入? Do you know of an example that does this?你知道这样做的例子吗?

UPDATE更新

The validation I am talking about is the compare validation (all other validation I know how to do, there are a lot of examples on the internet).我所说的验证是比较验证(我知道如何做的所有其他验证,互联网上有很多示例)。 However I did not found a client validation for having two fields that are connected with compare.但是,我没有找到具有与比较连接的两个字段的客户端验证。

Basically how do you do client validation for min that it has a value less than max and a validation for max that it has a value less than min.基本上你如何对 min 进行客户端验证,它的值小于 max 和 max 的验证,它的值小于 min。 Or what is the recommended alternatives to this.或者推荐的替代方法是什么。

NEW UPDATE新更新

It seems that the first phrase of my question has been lost.我的问题的第一句话似乎已经丢失了。 On a form I have two input fields lets say FieldA and FieldB.在一个表单上,我有两个输入字段,可以说 FieldA 和 FieldB。 FieldA has to be less than FieldB and FieldB has to greater than FieldA. FieldA 必须小于 FieldB,FieldB 必须大于 FieldA。

My question refers to: Is there an example on how to do this kind of validation.我的问题是:是否有关于如何进行此类验证的示例。 Since the common response is to do validation on a client then my question would be what validation should I add to the two fields?由于常见的响应是在客户端上进行验证,那么我的问题是我应该向这两个字段添加什么验证? Do I make both field not valid when the conditions are not met.当条件不满足时,我是否使两个字段都无效。 If yes then how do I make both fields valid again after the user has changed one of the fields.如果是,那么在用户更改其中一个字段后如何使这两个字段再次有效。

Both.两个都。

The reason:原因:

  • client validation only: your server will be hacked.仅客户端验证:您的服务器将被黑客入侵。
  • server side only: your users will be annoyed, although it's better than client side only仅服务器端:您的用户会生气,尽管它比仅客户端更好
  • both: happy users, not hack-able.两者:快乐的用户,无法破解。 A little bit of more work though.不过还有一点点工作。

As for the techniques, there are loads, depending on your setup.至于技术,有很多负载,具体取决于您的设置。 You could use DataAnnotations with validation attributes server side, and there are tons of jquery , angular , knockout etc. validation plugins for client side javascript.您可以将DataAnnotations与服务器端的验证属性一起使用,并且客户端 javascript 有大量的jqueryangularknockout等验证插件。


Addition: As @bradbury9 states: server side validation "fairly easy" 添加:正如@bradbury9 所述: 服务器端验证“相当容易”

An example of jQueries validation can be found here :可以在此处找到 jQueries 验证的示例:

 <script> $(document).ready(function(){ $("#commentForm").validate({ rules: { name: "required", email: { required: true, email: true, }, comment: "required" } });}); </script>

note : as stated before: client side validation is highly dependent on the technique you are using.注意:如前所述:客户端验证高度依赖于您使用的技术。 For AngularJs there is eg: this library.对于 AngularJs,有例如:这个库。


update更新

On your request, for a min max, aka range validation, an example with attributes (server-side):根据您的要求,对于最小最大值,又名范围验证,具有属性的示例(服务器端):

 //model public class Foo { [Range(1, 100)] [DataType(DataType.Currency)] public decimal Price { get; set; } }

And the controller:和控制器:

 // POST: Movies/Create [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Create(Foo foo) { if (!ModelState.IsValid) { //error } }

And the javascript side:和 javascript 方面:

 $( "#commentForm").validate({ rules: { field: { required: true, range: [13, 23] } } });

See source for details.有关详细信息,请参阅来源

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

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