Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Date Format Validation

Pattu
Pattu Posts: 45

I need to validate this format of the date in a field
Example: 2028-07-18 00:00:00.0

Best Answer

  • benken_parasoft
    benken_parasoft Posts: 1,318 ✭✭✭
    Answer ✓

    When I want to look up a regular expression for date and time I usually copy the one from the XML Schema spec: https://www.w3.org/TR/xmlschema11-2/#nt-dateTimeRep

    It isn't the same as the format you are describing but you can base it on that one. You probably want something like this:

    ([1-9][0-9]{3,}|0[0-9]{3})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]) (([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\.[0-9]+)?|(24:00:00(\.0+)?))
    

Answers

  • Pattu
    Pattu Posts: 45

    In SOA test actually

  • benken_parasoft
    benken_parasoft Posts: 1,318 ✭✭✭

    You could use a regular expression to check if the text matches an expected pattern.

    If you are doing the assertion with a Diff tool in Text mode then you can select "Regular Expression" on the Options tab.

    If you are doing the assertion with an XML/JSON Assertor tool then you can add a "Regular Expression Assertion".

  • Pattu
    Pattu Posts: 45
    edited May 2024

    I tried Regular expression but there are no options to validate the time and date format. The options available in drop down of Regular Expression are equal to , not equal to , greater than etc,..

  • benken_parasoft
    benken_parasoft Posts: 1,318 ✭✭✭

    Concerning "the time and date format" there is no single standard date and time format. You need to provide a regular expression any time you want to check if some text matches a particular expected pattern, whether it is some particular date-time pattern or the pattern for something else.

  • Pattu
    Pattu Posts: 45

    oh thank you very much I understood now, it worked

  • Pattu
    Pattu Posts: 45

    hi @benken_parasoft
    in my test , there is a value in XML like below and we need to validate the characters should not exceed 11 characters in total including decimals. Now the REG exg assertion should match "^-?\d{1,11}(.\d{1,2})?$|^-?\d{1,11}$" failed with error : Error Message:
    Regular Expression Assertion: Actual value, 0.0591, must match the provided regular expression:
    ^-?\d{1,11}(.\d{1,2})?$|^-?\d{1,11}$ with case sensitivity enforced

    Additional Details:
    Actual value, 0.0591, must match the provided regular expression:
    ^-?\d{1,11}(.\d{1,2})?$|^-?\d{1,11}$ with case sensitivity enforced


    The actual xpah i need to validate is from an XML

    0.0591

  • benken_parasoft
    benken_parasoft Posts: 1,318 ✭✭✭
    edited February 11

    we need to validate the characters should not exceed 11 characters in total

    You can do this at least a couple different ways using the XML Assertor tool. You could create a Regular Expression Assertion with regex like ".{0,11}". You could also create a Value Assertion with XPath like "string-length(/path/to/element/string()) <= 11" with expected value of "true".