Epic - Validate input size


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Epic - Validate input size
# 1  
Old 11-13-2012
Wrench Epic - Validate input size

Is there an easy way to validate an input field size. Let us say a script is asking to enter 10 digits mobile number, how do I write a script to validate it is numeric and is 10 digits in length? I just need an easy way w/o using looks ...etc. Is there such a away ?

Here is what I have so far and it looks like it is working, but is there an easier way?

Code:
print -n "Enter your Mobile 10 digit number to turn off ....? " ; read mobile
if  echo $mobile | egrep -q '^[0-9]+$' ; then
 echo "You have entered = $mobile"
else
 echo "IF-Invalid number <$mobile> entered. Entry must be numeric ..."
 exit 255
fi
echo  "..................."
case $mobile in
     [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]) echo "You have entered = $mobile" ;;
         *) echo "Mobile number <$mobile> size entered is not a 10 digits number ...."
            exit 255;;
esac


Last edited by mrn6430; 11-13-2012 at 03:25 PM..
# 2  
Old 11-13-2012
Code:
if echo $mobile | egrep -q '^[0-9]{10}$'
then
    # Number entered has 10 digits
else
    # Not a number or does not have 10 digits
fi

# 3  
Old 11-13-2012
Code:
read mobile

if [[ "$mobile" != +([0-9]) ]]
then
        # Not numeric
else
        # Is numeric
fi

if [[ ${#mobile} -eq 10 ]]
then
      # Is 10 digits
else
      # Not 10 digits
fi

# 4  
Old 11-13-2012
I also have another question:

After I validate the number, i need to find it in a file which has this format:


Data file:
8165886666@myairmail.com
8164445555@myairmail.com

How do I search for 816444555 in the file and check if it starts with a # "Commented out" and if it does skip it. Else a # in front of it in the same input file?

Thanks

Last edited by mrn6430; 11-13-2012 at 04:13 PM..
# 5  
Old 11-14-2012
Try
Code:
$ grep "$mobile" file
#8164445555@myairmail.com
8164445555@myairmail.com
$ grep "^$mobile" file
8164445555@myairmail.com

# 6  
Old 11-14-2012
Code:
numbers()
{
  str="$1"
  len=$2
  # replace all numbers with nothing = rest is not numbers
  notnumbers=${str//[0-9]/}
  [ "$notnumbers" != "" ] && return 1
  [ ${#str} != $len ]  && return 2
  return 0
}

#####

for tst in abc123 0123456789 123ABC123A 5555666666 677676767676 66262626266
do
        numbers "$tst" 10 && echo "$tst OK" || echo "$tst not ok"
done

# 7  
Old 11-14-2012
If you're using bash or ksh, you could read -N10 mobileto read exactly 10 chars. mobile=${mobile//[^[:digit:]]/} will eliminate any non-digit character in mobile, so you could check its length or do any other check...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Validate input files daily

We have a job which we need to run on daily bases, before loading data in a table we need to validate whether the input file is received or not. Daily client will place the files in a particular path.Below files which I need to process for 04/01/2013(Load date).... (2 Replies)
Discussion started by: katakamvivek
2 Replies

2. Shell Programming and Scripting

Validate input files and update

We have a job which we need to run on daily bases, before loading data in a table we need to validate whether the input file is received or not.Inputfile formatsrc_sps_d_Call_Center_Reporting_yyyymmdd_01.dat SPS-Service nameYYYY-yearMM-MonthDD-dayLike above we will get n number of files for... (1 Reply)
Discussion started by: katakamvivek
1 Replies

3. Shell Programming and Scripting

How to validate user's input..?

$Input_filename=$ARGV; if (!-d $Input_filename && ! -e $Input_filename) { print "USAGE: Please enter '$ABCD/def/dsed.txt' as an arguement \n"; exit; } 1. Input Is suppose to be something like "$ABCD/def/dsed.txt". if the input is wrong the script should throw an ERROR message.... (2 Replies)
Discussion started by: Rashid Khan
2 Replies

4. Shell Programming and Scripting

Another validate input Question.

I'm writing a bash shell script to 'help' me post to susepaste (I can NEVER remember the time options). Here's the code: #!/bin/bash ########## # # Project : personal script. # Started : Wed Aug 03, 2011 # Author : Habitual # Description : susepaste c-li script with user... (5 Replies)
Discussion started by: Habitual
5 Replies

5. Shell Programming and Scripting

How to validate input parameters?

Hi, I wonder how I can know if the input parameters to the script are numbers or text Thanks (11 Replies)
Discussion started by: Gengis-Kahn
11 Replies

6. Shell Programming and Scripting

Validate and sort input

Hi, This will most likely be a simple answer. Currently I have a situation where my script will be sent various options: -o1 -o2 -oe3@somthing.com Now, if I want to run a certain command based on the option I am sent, I am doing the following. for o in $(echo $options) do if ... (3 Replies)
Discussion started by: stuaz
3 Replies

7. UNIX for Dummies Questions & Answers

BASH validate user input

Hey, im trying to validate a user input and need some help. The input needs to be just a single letter. Im using a case to so this eg: read answer case $answer in *) echo "OK" ;; *) echo "This is a number" read answer ;; *) echo... (2 Replies)
Discussion started by: 602chrislys
2 Replies

8. Shell Programming and Scripting

validate input

the user inputs names that have to be inside square brackets I want to check if the user puts the brackets and if not ask him to re-enter the names (9 Replies)
Discussion started by: DDoS
9 Replies

9. Solaris

Epic Editor was not able to obtain a license for your use. Feature Epic Editor :Licen

Epic Editor was not able to obtain a license for your use. Feature Epic Editor :License server is down (1 Reply)
Discussion started by: durgaprasadr13
1 Replies

10. Shell Programming and Scripting

How to validate input values

Hi How would i validate value of a variable whether it is number,date or string Thanks in advance Sas (5 Replies)
Discussion started by: SasDutta
5 Replies
Login or Register to Ask a Question

Featured Tech Videos