Date formatter for an input field which formats the given numbers in MM/DD/YYYY format which also accepts NA letters in Angular

I have a requirement where a user should enter date in "MM/DD/YYYY" if available or else should be able to enter "NA" in the input field. Here is the stackblitz link where I am trying to achieve this functionality.

[Formatter not Validation Regex]

Note: User should be able to enter numbers or only N and A in NA sequence in the input field. Date validation is not required but the formatted numbers should be in this way ../../…. OR NA.

1 thought on “Date formatter for an input field which formats the given numbers in MM/DD/YYYY format which also accepts NA letters in Angular”

  1. You need add an OR expression to your regex for contemplate the case of NA.

    Now, the regex will looks like this:

    ^([0-2][0-9]|(3)[0-1])(\/)(((0)[0-9])|((1)[0-2]))(\/)\d{4}|NA$
    

    In your regex, the two alternative are anchored separately:

    • ^([0-2][0-9]|(3)[0-1])(\/)(((0)[0-9])|((1)[0-2]))(\/)\d{4} MM/DD/YYYY
    • | Or
    • NA$ NA

    See the regex demo.

    It will acept:

    • NA
    • MM/DD/YYYY
    Reply

Leave a Comment