Android Question Edittext Date format

Discussion in 'Android Questions' started by Kiran Raotole, Jul 21, 2018.

  1. Kiran Raotole

    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
     
  2. Dabzy1978

    Dabzy1978 Member Licensed User

    Regex is your friend...

    Code:
    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: Jul 21, 2018
  3. MarkusR

    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.
     
  4. DonManfred

    DonManfred Expert Licensed User

  5. Kiran Raotole

    Kiran Raotole Active Member Licensed User

    how to use date input control
     
  6. MarkusR

    MarkusR Well-Known Member Licensed User

  7. Kiran Raotole

    Kiran Raotole Active Member Licensed User

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice