选择最小和最大的验证 [英] Validation for selecting a minimum and maximum

查看:45
本文介绍了选择最小和最大的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,用户必须输入一个最小值和一个最大值.对这种情况进行验证的建议是什么?仅服务器验证或客户端和服务器验证.

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?

更新

我正在谈论的验证是比较验证(我知道该怎么做的所有其他验证,互联网上有很多示例).但是,我没有找到具有两个与compare连接的字段的客户端验证.

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.

基本上,您如何对最小值小于max的min进行客户端验证,对最小值小于min的max进行客户端验证.或推荐的替代方法是什么?

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. Or what is the recommended alternatives to this.

新更新

我的问题的第一句话似乎已经丢失.在表单上,​​我有两个输入字段,比如说FieldA和FieldB.FieldA必须小于FieldB,而FieldB必须大于FieldA.

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 has to be less than FieldB and FieldB has to greater than 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.

推荐答案

两者都

原因:

  • 仅客户端验证:您的服务器将被黑客入侵.
  • 仅服务器端:虽然比仅客户端端更好,但您的用户会感到烦恼
  • 两者:满意的用户,无法破解.不过,还有更多工作要做.
  • 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.

对于这些技术,根据您的设置,会有负载.您可以在服务器端使用带有验证属性的 DataAnnotations ,并且有大量的 jquery angular nockout 等.客户端javascript的验证插件.

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.


添加:如@ bradbury9所述:服务器端验证非常容易"

可以找到jQueries验证的示例此处:

An example of jQueries validation can be found here:

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

注释:如前所述:客户端验证在很大程度上取决于您所使用的技术.对于AngularJ,例如:库.

note: as stated before: client side validation is highly dependent on the technique you are using. For AngularJs there is e.g.: this library.

根据您的要求,进行最小最大(即范围验证),并提供一个具有属性(服务器端)的示例:

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; }
} 

和控制器:

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

还有javascript方面:

And the javascript side:

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

有关详细信息,请参见来源.

See source for details.

这篇关于选择最小和最大的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆