验证日期的EditText框 [英] Validate date in edittext box

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

问题描述

我使用下面的code,以验证日期edittextbox.however在XML文件中输入我给它的输入型的日期。

I am using the following code to validate date entered in edittextbox.however in xml file I had given its input type date.

int i = validate(registerdate);

这是验证功能:

private int  validate(String registerdate) {

        String regEx =
            "^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$";
          Matcher matcherObj = Pattern.compile(regEx).matcher(registerdate);
          if (matcherObj.matches())
            {
                return 1;
            }
            else
            {
                return 0;
            }
    }

它给我的编译时间误差

its giving me compile time error at

String regEx =
            "^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$";

说无效的转义序列。

saying invalid escape sequences.

任何一个可以揣摩出我错了或验证日期前pression任何其他方式?

Can any one figure out where I am wrong or any other way to validate date expression?

推荐答案

Java将处理\在字符串中作为起始的<一个href="http://ehis.ebscohost.com.mms02.cerritos.edu/jwilson/java_language_resources/Java_Escape_Sequences.htm"相对=nofollow>转义序列。请确保您使用\,而不是(这样你得到的字符串中的实际字符\),你应该没问题。

Java will treat \ inside a string as starting an escape sequence. Make sure you use \ instead (so that you get an actual \ character in the string) and you should be ok.

快速更新:作为艾蒂安指出,如果你真的想在正则表达式本身就是一个\,你需要使用\\,因为这将产生\在字符串,这将产生\在正则表达式。

Quick Update: As Etienne points out, if you actually want a \ in the RegEx itself, you'll need to use \\, since that will produce \ in the string, which will produce \ in the RegEx.

修正后您的正则表达式:

Your regex after correction:

String regEx ="^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\\d{2}$";

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

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