简体   繁体   English

jQuery邮政编码clientvalidate

[英]Jquery Postal code clientvalidate

I am working on a customvalidator to validate and replace (if possible) a postal code. 我正在使用customvalidator来验证和替换(如果可能)邮政编码。 This is a Dutch postal code and should look like "5050 AA". 这是荷兰的邮政编码,应类似于“ 5050 AA”。 When the user enters "5050AA" this postal code should be replaced with "5050 AA". 当用户输入“ 5050AA”时,该邮政编码应替换为“ 5050 AA”。 I tried this by adding the following script to my page, which is called in the customvalidator: 我通过将以下脚本添加到我的页面(在customvalidator中调用)来尝试此操作:

 <script type="text/javascript">
            function Postcode_ClientValidate(source, arguments) {
                var val = arguments.Value
                var result = "";
                var myPCRegExp1 = new RegExp("^[1-9][0-9]{3}\s?[a-zA-Z]{2}$", "i");
                var myPCRegExp2 = new RegExp("(\d{4}) (\w{2})");    

                if ((!myPCRegExp1.test(val)) && (!myPCRegExp2.test(val))) {
                    arguments.IsValid = false;
                } else {
                    if (myPCRegExp1.test(val)) {
                        arguments.Value = val.replace(myPCRegExp1, "$1, $2");
                        arguments.IsValid = true;
                    } else if (myPCRegExp1.test(val)) {
                        arguments.IsValid = true;
                    }               
                }

                //jQuery("input#[HIERDEID]").val("Test");
            }
        </script>

However, the script above is picking up the "5038AA" but not the "5038 AA" as a match, so i can't validate a working postal code and can't rewrite to the valid postal code. 但是,上面的脚本将“ 5038AA”而不是“ 5038 AA”作为匹配项,因此我无法验证有效的邮政编码,也无法重写为有效的邮政编码。 What am I doing wrong? 我究竟做错了什么?

It's a standard .aspx page with a form and a customvalidator: 这是带有表单和customvalidator的标准.aspx页面:

Try battling it out with this tool: 尝试使用此工具进行对抗:

http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx for testing this sort of thing. http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx,用于测试此类内容。

"5038 AA" is matching myPCRegExp1, too, because of the '\\s?' 由于“ \\ s”,“ 5038 AA”也与myPCRegExp1匹配。

I think you need this: 我认为您需要这样做:

var myPCRegExp1 = new RegExp("^([1-9][0-9]{3})([a-zA-Z]{2})$", "i");

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

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