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);
this is validate function:
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.
Can any one figure out where I am wrong or any other way to validate date expression?
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:
I hope this will help :)
and put these codes on your onClick():