how do i validate a date input

how do i validate a date input

Post by Brian Ran » Wed, 09 Nov 1994 04:13:12



As I said I am trying to check that a valid date is being input,
will egrep take input from a pipe. There must be a better way to do this.
There must be cuz this dont work.

getadate=1      
while [ $getadate ]
do
        echo -n "Please enter date  mm/dd/yy. "
        read checkdate
datestatus=`echo "${checkdate}" | egrep -c '[01][0-9]/[0-3][0-9]/9[0-9]'`
        if [ $datestatus = 1 ]
        then
                getadate=0
        fi
done

THANX for any help
brian

 
 
 

how do i validate a date input

Post by Pierre Gaumo » Thu, 10 Nov 1994 03:40:50



>As I said I am trying to check that a valid date is being input,
>will egrep take input from a pipe. There must be a better way to do this.
>There must be cuz this dont work.

>getadate=1  
>while [ $getadate ]
>do
>    echo -n "Please enter date  mm/dd/yy. "
>    read checkdate
>datestatus=`echo "${checkdate}" | egrep -c '[01][0-9]/[0-3][0-9]/9[0-9]'`
>    if [ $datestatus = 1 ]
>    then
>            getadate=0
>    fi
>done

>THANX for any help
>brian

This will detect some errors but not all: 02/29/92 is ok, but not 02/29/93,
02/31/94 or 05/39/94. The following igives an idea how it could be checked
further:

m=`echo "$date" | awk -F/ ' {print $1}'`
d=`echo "$date" | awk -F/ ' {print $2}'`
y=`echo "$date" | awk -F/ ' {print $3}'`

nd="31 28 31 30 31 30 31 31 30 31 30 31"

if [ $m -gt 12 ]; then
  echo error
fi

if [ `expr $y '%' 4` -eq 0 -a $m -eq 2 ]; then
  l=29
else
  l=`echo "$nd" | nawk ' {print $m}' m=$m`
fi

if [ $d -gt $l ]; then
  echo error
fi

I hope this will help.

 
 
 

how do i validate a date input

Post by Praveen C. Pu » Thu, 10 Nov 1994 07:27:00


|> As I said I am trying to check that a valid date is being input,
|> will egrep take input from a pipe. There must be a better way to do this.
|> There must be cuz this dont work.
|>
|>
|> getadate=1        
|> while [ $getadate ]
|> do
|>   echo -n "Please enter date  mm/dd/yy. "
|>   read checkdate
|> datestatus=`echo "${checkdate}" | egrep -c '[01][0-9]/[0-3][0-9]/9[0-9]'`
|>   if [ $datestatus = 1 ]
|>   then
|>           getadate=0
|>   fi
|> done
|>
|> THANX for any help
|> brian
|>

Instead of setting getadate=0 at the end, set getadate=""
--
Praveen Puri
Domestic Project Group
Motorola
"Have sh, will travel"

 
 
 

how do i validate a date input

Post by Tom Hofma » Wed, 16 Nov 1994 01:49:02



>As I said I am trying to check that a valid date is being input,
>will egrep take input from a pipe. There must be a better way to do this.
>There must be cuz this dont work.

For a complete check against the Gregorian calendar do the following
ksh commands (the input date be in $DAY, $MONTH, and $YEAR,
respectively):

DATE="$YEAR $MONTH $DAY"
let "(MONTH=(MONTH+9)%12)>9&&(YEAR-=1)"
let "D=YEAR*365+YEAR/4-YEAR/100+YEAR/400+(MONTH*367+7)/12+DAY-1"
let YY=D "YY-=(YY+1)/146097" YY+=YY/36524 "YY-=(YY+1)/1461" YY/=365
let DOY=D-YY*365-YY/4+YY/100-YY/400 "MM=(DOY*12+4)/367"
let "DD=DOY+1-(MM*367+7)/12" "(MM=(MM+2)%12+1)<3&&(YY+=1)"
if [ "$DATE" = "$YY $MM $DD" ]
then echo "valid date"
else echo "unvalid date"
fi

Tom Hofmann

 
 
 

1. Validating input date

Is there a simple way to check whether an input is a valid date (say, mm/dd/yy)? I suppose I could write some verbose code, but I'm lazy.       ;-)

___Atish Sanyal___
All questions posted here are my own and do not reflect the stupidity of my employer

2. AudioCD's & Linux

3. verifying/validating input

4. How to remove the stored username password of a web side?

5. validating user input

6. Install printers

7. validating user input at server level

8. cant find gzip

9. case statement to validate input

10. validating a date

11. Validating unix login from DOS ?

12. input/output error when doing mkdir

13. How do a see what a user is doing (input/output to screen)