You may have a scenario like you need to type a date value on Text
Field and need to validate the date means, you can go for pattern, matcher
object.
Here is the code to validate a Date format,
- String dateRegEx = '(0?[1-9]|1[012])/(0?[1-9]|[12][0-9]|3[01])/((19|20)\\d\\d)';
- Pattern myPattern = Pattern.compile(dateRegEx);
- Matcher myMatcher = myPattern.matcher('12/01/2012');
- if(!myMatcher.matches())
- {
- ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, Please provide the date with the format MM/DD/YYYY'));
- }
dateRegEx - It holds the Regular expression to validate date value.
myPattern - Declares the Pattern object and compile the regular expression.
myMatcher - Matches the given value
by initializing matcher object.
No comments:
Post a Comment