Android Question Edittext Date format

Kiran Raotole

Active Member
Licensed User
I have to validate date format for edittext.
Like dont accept wrong date.
ex. 50/07/2018 == not accepted or 21/25/2018
 

Dabzy1978

Member
Licensed User
Regex is your friend...

B4X:
        Log(Regex.IsMatch("^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]|(?:Jan|Mar|May|Jul|Aug|Oct|Dec)))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)(?:0?2|(?:Feb))\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$","11/50/1978"))
The above will return false as obviously, theres no 50th month (See last parameter of Regex.IsMatch), though bang, say 05 in there and you'll get true as expected.

This also takes leap years into account, as well as other date formats... Check the original source page for more details.

Original source: https://stackoverflow.com/questions/15491894/regex-to-validate-date-format-dd-mm-yyyy

Dabz
 
Last edited:

MarkusR

Well-Known Member
Licensed User
or spilt it by / or - and make three variables day,month,year and check the range.
if they ok make a new date variable from it.

or use a date input control.
 
Top