简体   繁体   English

jQuery验证-如何添加规则?

[英]JQuery Validation - How to add rules?

I was trying to validate a textbox input via jquery, but I don't know how to apply certain rules for this kind of situation. 我试图通过jquery验证文本框输入,但是我不知道如何针对这种情况应用某些规则。

Here is the deal: I want the textbox to accept only alphanumeric inputs (AZ and 0-9) and using ' . 达成协议:我希望文本框仅接受字母数字输入(AZ和0-9)并使用'。 ' as separators for certain cases. '作为某些情况下的分隔符。 If the textbox has a value of 16 (in this case, then the user must've typed something like this: 67EA981XXVT110TP), then I want to validate to check if there's only letters and numbers in the input, but if the value is 19 (something like: 67EA.981X.XVT1.10TP) then it has to be checked to confirm that the separators are in the right position (every 4 character input) and if there is only alphanumeric values in the field. 如果文本框的值为16(在这种情况下,则用户必须键入如下内容:67EA981XXVT110TP),那么我想验证输入中是否只有字母和数字,但是值是否为19 (例如:67EA.981X.XVT1.10TP),则必须检查以确认分隔符在正确的位置(每4个字符输入),并且该字段中是否只有字母数字值。

This is what I was trying to do (I'm using Razer Engine View) 这就是我想要做的 (我正在使用Razer Engine View)

            <script type="text/javascript">
                $(function () {
                    $('#txtCode').blur(function () {
                        if ($('#txtCode').val().length > 19) {
                            alert('Invalid field input!')
                        }
                        else if ($('#txtCode').val().length >= 1 && $('#txtCode').val().length < 16) {
                            alert('Invalid field input!')
                        }

                        if ($('#txtCodVerificador').val().length == 16) {
                            //check if there is only numbers and letters in the field
                        }

                        if ($('#txtCodVerificador').val().length == 19) {
                            //check if there is only numbers and letters in the field and if the separators are in the right place (every 4 character input)
                        }
                    });
                });
            </script>

You have mentioned I'm using Razer Engine View so I assume this is . 您已经提到我正在使用Razer Engine View,所以我假设这是 Your current implementation means that you need to repeat all you validation again on the server when you submit. 当前的实现意味着您提交时需要在服务器上再次重复所有验证。 To handle this all out of the box, add a RegularExpressionAttribute to your property 要开箱即用,请在您的媒体资源中添加RegularExpressionAttribute

[RegularExpression(@"^[A-Z0-9]{16}|([A-Z0-9]{4}\.){3}[a-zA-Z0-9]{4}$", ErrorMessage = "...")]
public string Code { get; set; }

and in the view 并认为

@Html.TextBoxFor(m => m.Code)
@Html.ValidationMessageFor(m => m.Code)

If your view includes jquery.validate.js and jquery.validate.unobtrusive.js , then you get both client and server side validation (your $('#txtCode').blur(.. script is not required) 如果您的视图包括jquery.validate.jsjquery.validate.unobtrusive.js ,那么您将同时获得客户端和服务器端验证( $('#txtCode').blur(..脚本)

Credit to ArcaneCraeda's answer for the regex. 感谢ArcaneCraeda对正则表达式的回答。

Give this a shot: 试一下:

$(function () {
  $('#txtCode').blur(function () {
    if ($('#txtCode').val().match(/^[A-Z0-9]{16}$/)) {
      console.log("Matches the 16");
    }else if ($('#txtCode').val().match(/^([A-Z0-9]{4}\.){3}[a-zA-Z0-9]{4}$/)) {
      console.log("Matches the 19");
    }else{
      console.log("Does not match!");
    }
  });
});

JSFiddle 的jsfiddle

If you have any questions about the regex, ask away! 如果您对正则表达式有任何疑问,请走开!

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

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