Need help with Regular Expression Validation

Discussion in 'OT Technology' started by sideshow bob, Aug 22, 2003.

  1. sideshow bob

    sideshow bob New Member

    Jun 30, 2003
    Likes Received:
    Newb question

    I'm using a regular expression to validate some inputted data.

    Two questions

    1) What is the expression to validate this date format MMDDYYY, no spaces, dashes or slashes

    2) What is the expression to validate a two character field, where the first character can be text or numeric, but the second will always be numeric.

  2. ChosenGSR

    ChosenGSR Mama always said you'd be the chosen one

    Oct 24, 2001
    Likes Received:
    HoCo, MD
    1) will be a long expression because you need to the check for a legit date, even then you cant "really" do it correctly due to feb. having say 27 days one year...
    2) "\w\d"
  3. Astro

    Astro Code Monkey

    Mar 18, 2000
    Likes Received:
    Cleveland Ohio
    For date validation, I think you will find regular expressions are going to come up short. I think realistically, the best you will be able to do with regular expressions is verify the date is all numeric. Depending on the language, you will want to verify the date range is a correct, such as:

    July 31, 2003 is legit
    June 31, 2003 is not - I don't think you can easily have a regular expression to check this - although with some sort of language (what language you using?) you can do some date tricks and determine if its legit. PHP has strtotime() and date(), Java has SimpleDateFormat(), ASP has... um... it has a date() object, but you have to write a chunk of code to get it to do what you want (you'll end up having to extract month, day, & year and then re-assemble with the date object and see if it blows up or not).

    Make sense?

    So, if its just number validition and making sure the user didn't enter text or something not date friendly, then regular expressions is for you. If you actually want to test the date and make sure its legit, then its best if you find another approach...

Share This Page