#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    4
    Rep Power
    0

    Question Number & Date Validation Query


    Hi all,
    Do you have any pointers how to validate numbers (not to contain alphabets and special characters) and date(MM/DD/YYYY) format.

    I used following regular expression to validate integer, which is not working in the default shell:

    nodigits="$(echo $testvalue | sed 's/[[:digit:]]//g')"

    if [ ! -z $nodigits ] ; then
    echo "Invalid number format! Only digits, no commas, spaces, etc." >&2
    return 1
    fi


    Alok
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Middle Europa
    Posts
    1,200
    Rep Power
    14
    why not simply use (old and) powerfull tools ?

    case x$input in x[0-9]*[0-9]) ;; *) print an error mssg and exit ;; esac
    do the rest
    exit 0

    NOTA: this works w/ ALL shells (only csh needs switch instead of case, but csh
    never was a good scripting shell, so don't use csh for scripts)
  4. #3
  5. No Profile Picture
    ......@.........
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2004
    Posts
    1,345
    Rep Power
    56
    regular expressions don't do much for dates. You can either write a simple C module
    that calls getdate() which does validate dates, or use another module that already does it. For example, touch validates dates.

    Here is a primitive example that validates a full date and time in YYYYMMDDhhmmss format.

    Code:
    #!/bin/ksh
    # validate date/time
    MMDD=$(expr substr "$*" 1 4)
    YYYY=$(expr substr "$*" 5 4)
    hh=$(expr substr "$*" 10 2)
    mm=$(expr substr "$*" 13 2)
    ss=$(expr substr "$*" 16 2)
    if touch -c -t $YYYY$MMDD$hh$mm.$ss dummyfile
    then
       echo valid
    else
       echo invalid
    
    fi
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Prague, Czech Rep.
    Posts
    117
    Rep Power
    13
    Try this to check the date string:

    :
    [ $# -eq 1 ] || {
    echo "$0: usage: $0 MM/DD/YYYY" >&2
    exit 2
    }

    case $1 in
    [0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]) : ;;
    *) echo invalid
    exit 1 ;;
    esac

    IFS=/ # redefine internal field separators
    set $1 # separate the fields into $1, $2, $3

    # touch does not check for 31st of February.
    # If necessary, write a better check
    if touch -ct $3$2$10000 x
    then
    echo valid
    else
    echo invalid
    exit 1
    fi


    Regads
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Prague, Czech Rep.
    Posts
    117
    Rep Power
    13
    The test for integer from guggach

    "
    case x$input in x[0-9]*[0-9]) ;; *) print an error mssg and exit ;; esac
    do the rest
    exit 0
    "

    is not OK, because it evaluates "1XXX2" as valid.

    In Korn shell you can use:

    case $1 in
    +([0-9])) echo valid ;;
    *) echo invalid ;;
    esac

    In Bourne shell you can count all characters and all digits. If the counts are equal, the input is an integer:

    C=`expr $1 : ".*"`
    D=`expr $1 : "[0-9][0-9]*"`
    [ $C -eq $D ] && {
    echo valid
    }

    Regards
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    1
    Rep Power
    0

    A slick one liner


    None of those methods does much for validating that the date is a valid date. Feb 32 is validated as Mar 3. I took it and used cal, sed , and grep to validate a recovery date in CCYY/MM/DD:

    #Validate that the cycle date is a real date
    MM=$(expr substr "$recovery" 6 2)
    DD=$(expr substr "$recovery" 9 4)
    YYYY=$(expr substr "$recovery" 1 4)
    D=`echo $DD|sed s/^0//`
    cal $MM $YYYY|tail +3|grep $D>/dev/null
    if [ ${?} -ne 0 ]; then
    echo "ERROR: Invalid recovery date. Process end."
    exit 1
    fi


    The heart of this is a one-liner:
    cal $MM $YYYY|tail +3|grep $D>/dev/null

    Everything else is window dressing.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Middle Europa
    Posts
    1,200
    Rep Power
    14

    so so


    The test for integer from guggach

    "
    case x$input in x[0-9]*[0-9]) ;; *) print an error mssg and exit ;; esac
    do the rest
    exit 0
    "

    is not OK, because it evaluates "1XXX2" as valid.
    prouve it
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0
    Whilst using touch does allow invalid dates at the end of non-31 day months, using cal does the opposite, ie it allows 00 as a valid day. If you use both, you cover all eventualities, eg :-

    echo "\nPlease enter Date and Time, in format DD/MM/YYYY HH24:MI : \c"
    read fbt

    echo ${fbt} |grep -q '^[0-3][0-9]/[0-1][0-9]/20[0-9][0-9] [0-2][0-9]:[0-5][0-9]$'
    status=$?

    if [[ ${status} -ne 0 ]]
    then
    echo "you have entered it wrong - try again and take more care!"
    exit 1
    else
    echo "The syntax looks ok, now lets check if its a valid date"
    D=`echo ${fbt}|cut -c1-2`
    M=`echo ${fbt}|cut -c4-5`
    Y=`echo ${fbt}|cut -c7-10`
    h=`echo ${fbt}|cut -c12-13`
    m=`echo ${fbt}|cut -c15-16`
    if [ `touch -c -t ${Y}${M}${D}${h}${m}.00 dummyfile` ]
    then
    DD=`echo ${D}|sed s/^0//`
    cal ${M} ${Y}|tail +3|grep ${DD} >/dev/null
    status=${?}
    if [ ${status} -eq 0 ]; then
    echo valid
    else
    echo invalid
    fi
    else
    echo invalid
    fi
    fi

IMN logo majestic logo threadwatch logo seochat tools logo