Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

User Input Validation 1

Status
Not open for further replies.

nithink

Programmer
Nov 7, 2002
92
US
Hi,
Help needed abt validation. I need to validate the user-input.
I require the user should input as mm/dd/yy. So he should be able to enter in this format only. So until he enters in this format i should not allow him to enter any other values. And I need to check he enters only numbers
for mm,dd,yy.

I tried like this, but didnt work out properly. Is this rite way ? Am pretty new to Unix.

echo "enter date in mm/dd/yy "
read sdt
if [ $sdt != [0-9][0-9]/[0-9][0-9]/[0-9][0-9] ]
then
echo "enter again"
exit 1
fi

Thanks in Advance.
 
Here's a version I wrote and use (mm and dd are reversed here in the UK)

SL1=`echo ${INPUT} | cut -c3,3`
SL2=`echo ${INPUT} | cut -c6,6`
if [ "${SL1}" = "/" -a "${SL2}" = "/" ]
then
continue
else
echo '\nDate must be dd/mm/yy please\n\n'
ERROR=1
exit ${ERROR}
fi
if [ ${#INPUT} -eq 8 ]
then
YEAR=`echo ${INPUT} | cut -c7,8`
elif [ ${#INPUT} -eq 10 ]
then
YEAR=`echo ${INPUT} | cut -c9,10`
else
echo '\nDate must be dd/mm/yy (8 chars) please\n\n'
ERROR=1
exit ${ERROR}
fi

DATE=`echo ${INPUT} | cut -c1,2`
MONTH=`echo ${INPUT} | cut -c4,5`

if [ ${MONTH} -gt 12 -o ${MONTH} -lt 1 ]
then
echo '\nEnter a Month between 01 - 12 please\n\n'
ERROR=1
exit ${ERROR}
fi
case $MONTH in
01|03|05|07|08|10|12)

if [ ${DATE} -gt 31 -o ${DATE} -lt 1 ]
then
echo '\nEnter a Day between 01 - 31 please\n\n'
ERROR=1
exit ${ERROR}
fi
;;
04|06|09|11)
if [ ${DATE} -gt 30 -o ${DATE} -lt 1 ]
then
echo '\nEnter a Day between 01 - 30 please\n\n'
ERROR=1
exit ${ERROR}
fi
;;
02)
MODU=`expr ${YEAR} % 4`
if [ ${MODU} -eq 0 ]
then
if [ ${DATE} -gt 29 -o ${DATE} -lt 1 ]
then
echo '\nEnter a Day between 01 - 29 please\n\n'
ERROR=1
exit ${ERROR}
fi
else
if [ ${DATE} -gt 28 -o ${DATE} -lt 1 ]
then
echo '\nEnter a Day between 01 - 28 please\n\n'
ERROR=1
exit ${ERROR}
fi
fi
;;
esac


HTH !!!!

Dickie Bird (:)-)))
 
Thanks Dickie that was awesome..

I've another question.. Suppose he enters a wrong value, it
gives error message and exits. What can be done to make him to input again without exitting till he enters a correct value ?

Thanks much...
 
Try something like this:
Code:
while echo "enter date in mm/dd/yy \c"; do
  read sdt
  case $sdt in [0-9][0-9]/[0-9][0-9]/[0-9][0-9])
    break;;
  esac
  echo "'$sdt' is not a valid date, try again"
done

Hope This Help
PH.
 
An easy way to trap the error and get the operator to retry is to make Dickie Bird's code (above) into a function. I say Dickie Birds because his error checking is much more rigorous!

Example:
#!/bin/sh

#### Functions #####
SetDate
{
.
Dickie Bird's code
.
}
#####################

# call to SetDate function
SetDate
.
.
.

**EXCEPT** replace the lines
ERROR=1
exit ${ERROR}
with a call back to the function

====================================

#!/bin/sh

SL1=`echo ${INPUT} | cut -c3,3`
SL2=`echo ${INPUT} | cut -c6,6`

SetDate
{
if [ "${SL1}" = "/" -a "${SL2}" = "/" ]
then
continue
else
echo '\nDate must be dd/mm/yy please\n\n'
SetDate
fi
if [ ${#INPUT} -eq 8 ]
then
YEAR=`echo ${INPUT} | cut -c7,8`
elif [ ${#INPUT} -eq 10 ]
then
YEAR=`echo ${INPUT} | cut -c9,10`
else
echo '\nDate must be dd/mm/yy (8 chars) please\n\n'
SetDate
fi

DATE=`echo ${INPUT} | cut -c1,2`
MONTH=`echo ${INPUT} | cut -c4,5`

if [ ${MONTH} -gt 12 -o ${MONTH} -lt 1 ]
then
echo '\nEnter a Month between 01 - 12 please\n\n'
SetDate
fi
case $MONTH in
01|03|05|07|08|10|12)

if [ ${DATE} -gt 31 -o ${DATE} -lt 1 ]
then
echo '\nEnter a Day between 01 - 31 please\n\n'
SetDate
fi
;;
04|06|09|11)
if [ ${DATE} -gt 30 -o ${DATE} -lt 1 ]
then
echo '\nEnter a Day between 01 - 30 please\n\n'
SetDate
fi
;;
02)
MODU=`expr ${YEAR} % 4`
if [ ${MODU} -eq 0 ]
then
if [ ${DATE} -gt 29 -o ${DATE} -lt 1 ]
then
echo '\nEnter a Day between 01 - 29 please\n\n'
SetDate
fi
else
if [ ${DATE} -gt 28 -o ${DATE} -lt 1 ]
then
echo '\nEnter a Day between 01 - 28 please\n\n'
SetDate
fi
fi
;;
esac
}

# Rest of the script
|
|
V

#call to SetDate function
SetDate
|
|
V
# End script


Good Luck,
Duane
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top