jQuery邮政编码clientvalidate [英] Jquery Postal code clientvalidate

查看:115
本文介绍了jQuery邮政编码clientvalidate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用customvalidator来验证和替换(如果可能)邮政编码. 这是荷兰的邮政编码,应类似于"5050 AA".当用户输入"5050AA"时,该邮政编码应替换为"5050 AA".我通过将以下脚本添加到我的页面中(在customvalidator中调用该脚本)来进行了尝试:

I am working on a customvalidator to validate and replace (if possible) a postal code. This is a Dutch postal code and should look like "5050 AA". When the user enters "5050AA" this postal code should be replaced with "5050 AA". I tried this by adding the following script to my page, which is called in the 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>

但是,上面的脚本将"5038AA"而不是"5038 AA"作为匹配项,因此我无法验证有效的邮政编码,也无法重写为有效的邮政编码. 我在做什么错了?

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. What am I doing wrong?

这是带有表单和customvalidator的标准.aspx页面:

It's a standard .aspx page with a form and a customvalidator:

推荐答案

尝试使用此工具进行对抗:

Try battling it out with this tool:

http://derekslager. com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx 来测试这种事情.

这篇关于jQuery邮政编码clientvalidate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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