Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Regular expression and dates.

  • 12-09-2002 08:02AM
    #1
    Closed Accounts Posts: 192 ✭✭


    Hi all,
    I've been having a spot of bother with a regular expression that I need to use. I have a text field that I need to validate the correct formats are as follows
    dd/mmm/yyyy
    12/feb/2002
    ('empty')
    yes the third one is an empty string. Currently I have the first and second working but cant figure out the empty string, the regular expression i'm using is
    ([0-9d]{2}\/[A-Za-z]{3}\/[0-9y]{4})
    can anyone tell me how to check for the empty string as well
    Regards,
    Clamor.


Comments

  • Banned (with Prison Access) Posts: 16,659 ✭✭✭✭dahamsta


    Wouldn't it just be:
    ([0-9d]{2}\/[A-Za-z]{3}\/[0-9y]{4}|)
    
    (Note pipe on the end.)

    By the way, your regex will still allow anomalies, people will be able to mix and match, for example with a year like 200y.

    adam


  • Closed Accounts Posts: 192 ✭✭Clamor


    Good point.
    I'll need to tighten it a bit more.
    Adding the pipe at the end though allows everything to get through.
    I've changed the expression so it now looks like
    (^\s*$)|([0-9d]{2}\/[A-Za-z]{3}\/[0-9y]{4})
    This catches the empty string or the date format now. I still need to sort out the issue you brought up though.


  • Closed Accounts Posts: 192 ✭✭Clamor


    This should work now.
    (^\s*$)|([0-9]{2}\/[A-Za-z]{3}\/[0-9]{4})|([d]{2}\/[m]{3}\/[y]{4})
    :)


Advertisement