Operating System - HP-UX
1752497 Members
5632 Online
108788 Solutions
New Discussion юеВ

Re: Input Validation of comma separated values

 
SOLVED
Go to solution
Patrick Ware_1
Super Advisor

Input Validation of comma separated values

Hello all,

I am working on a script and have the first part solved of numerical input validation. Below the code validates that the input is a numerical value between 100 and 1000. If not, it errors out. Now I need to be able to read values separated by a comma. For example, instead of my input being let's say 105, I'd like for the input validated to be more like 105,106,500. How can I achieve this masters of scripting?

Current code for custom function:

get_info_check ()
{
echo
echo "Which INFO check would you like to run?"
echo "The valid range is 100-999."
print -n "Enter INFO number : "
read INFO_CHK
#INPUT VALIDATION HERE!!!
if [[ "$INFO_CHK" != +([0-9]) ]]
then
echo
echo "ERROR: You must enter a valid number!!"
get_info_check
elif [ "$INFO_CHK" -ge 100 -a "$INFO_CHK" -le 999 ]
then
echo "GOOD JOB!!!"
else
echo
echo "ERROR: Valid range is 100-999!!"
get_info_check
fi
}
7 REPLIES 7
klb
Valued Contributor

Re: Input Validation of comma separated values





New script with changes:
-----------------------CUT HERE-----------
get_info_check ()
{
echo
echo "Which INFO check would you like to run?"
echo "The valid range is 100-999."
print -n "Enter INFO number : "
read INFO_CHK
echo $INFO_CHK | awk -v FS=',' '{
split($0,mystring,",")
for ( i=1;i<=NF;i++ ) {
print mystring[i];
}
}' | while read INPUT;do
#INPUT VALIDATION HERE!!!
if [[ "$INPUT" != +([0-9]) ]]
then
echo "input: ${INPUT}: not valid number"
elif [ "$INPUT" -ge 100 -a "$INPUT" -le 999 ]
then
echo "input: ${INPUT}: GOOD JOB!!!"
else
echo "input: ${INPUT}: ERROR: Valid range is 100-999!!"
echo
fi
done
}

get_info_check

-----------------CUT HERE--------------

Example run:
Which INFO check would you like to run?
The valid range is 100-999.
Enter INFO number : 100,kbe,200,kb,300,400,600
input: 100: GOOD JOB!!!
input: kbe: not valid number
input: 200: GOOD JOB!!!
input: kb: not valid number
input: 300: GOOD JOB!!!
input: 400: GOOD JOB!!!
input: 600: GOOD JOB!!!

This will take any number of comma separated entries. It will choke on null entries, leading or trailing comma.

Hth,

-klb
James R. Ferguson
Acclaimed Contributor

Re: Input Validation of comma separated values

Hi Patrick:

You might use something like this:

# cat ./mycheck
get_info_check ()
{
echo
echo "Which INFO check would you like to run?"
echo "The valid range is 100-999."
print -n "Enter INFO number : "
read INFO_CHK
#INPUT VALIDATION HERE!!!
if [[ "$INFO_CHK" != +([0-9]) ]]
then
echo
echo "ERROR: You must enter a valid number!!"
get_info_check
elif [ "$INFO_CHK" -ge 100 -a "$INFO_CHK" -le 999 ]
then
echo "GOOD JOB!!!"
else
echo
echo "ERROR: Valid range is 100-999!!"
get_info_check
fi
}

...run as:

# ./mycheck 100 1000 666 123456
'100' is valid
ERROR: '1000' must be 100-999
'666' is valid
ERROR: '123456' must be 100-999

Regards!

...JRF...
Patrick Ware_1
Super Advisor

Re: Input Validation of comma separated values

Thanks for your reply. I guess one thing I forgot to mention above is that I only want the input line, because it will be passed off to another script to decide what to do with the input. For example, I want it to echo "GOOD JOB!!!' only when the criteria are met, and error out if any of the comma separated fields are not valid.
Patrick Ware_1
Super Advisor

Re: Input Validation of comma separated values

Thank you for that reply. I want to validate the input as numbers between 99 and 1000, and keep the input such as 105,106,500 in that format, so it can be passed off as an option to another script. Is this possible?
klb
Valued Contributor
Solution

Re: Input Validation of comma separated values


this should do:
-----------------------CUT------


get_info_check ()
{
ERRORS=0
echo
echo "Which INFO check would you like to run?"
echo "The valid range is 100-999."
print -n "Enter INFO number : "
read INFO_CHK
echo $INFO_CHK | awk -v FS=',' '{
split($0,mystring,",")
for ( i=1;i<=NF;i++ ) {
print mystring[i];
}
}' | while read INPUT;do
#INPUT VALIDATION HERE!!!
if [[ "$INPUT" != +([0-9]) ]]
then
ERRORS=$((ERRORS+1))
elif [ "$INPUT" -lt 100 -o "$INPUT" -gt 999 ]
then
ERRORS=$((ERRORS+1))
fi
done
if [ $ERRORS -ne 0 ];then
echo "input had $ERRORS errors"
else
echo "GOOD JOB!!!"
echo "send input to next stage: $INFO_CHK"
fi
}

get_info_check

---------------CUT

example run:
1)
Which INFO check would you like to run?
The valid range is 100-999.
Enter INFO number : 100,200,300
GOOD JOB!!!
send input to next stage: 100,200,300

2)
Which INFO check would you like to run?
The valid range is 100-999.
Enter INFO number : 100,klb,200,666,1,2,3,johndoe
input had 5 errors

James R. Ferguson
Acclaimed Contributor

Re: Input Validation of comma separated values

Hi (again) Patrick:

My apologies! I re-posted *your* script with *my* output! Oops. I *meant* to offer:

# cat ./mycheck
#!/bin/sh
get_info_check ()
{
typeset INFO_CHK=$1
if [ $(expr "${INFO_CHK}" : '[0-9]*') -ne $(expr "${INFO_CHK}" : '.*') ]
then
echo "ERROR: '${INFO_CHK}' be all digits"
fi
if [ "$INFO_CHK" -ge 100 -a "$INFO_CHK" -le 999 ]; then
echo "'${INFO_CHK}' is valid"
else
echo "ERROR: '${INFO_CHK}' must be 100-999"
fi
}
NUMS=$(echo "$@"|sed -e 's/,/ /g')
for NUM in ${NUMS}
do
get_info_check ${NUM}
done
exit 0

...run as:

# ./mycheck 100 1000 666 123456
'100' is valid
ERROR: '1000' must be 100-999
'666' is valid
ERROR: '123456' must be 100-999

Regards!

...JRF...
Patrick Ware_1
Super Advisor

Re: Input Validation of comma separated values

klb,

I meant to give you 10 points....