Validating the input date format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Validating the input date format
# 1  
Old 07-31-2008
Validating the input date format

I have one script.for that the inputs are fromdate(dd/mon/yyyy) and todate(dd/mon/yyyy).
How i can validate the input format?for eg.27/08/2008 is not valid.27/aug/2008 or 27/Aug/2008 are valid.
and the todate is optional.so if the todate is not present in the input then i need to assign the current date as the
todate in the format-dd/mon/yyyy.how to do this.
# 2  
Old 07-31-2008
What have you tried so far? As a start, you can use case/esac to check the input.
# 3  
Old 08-01-2008
Could you please tell me how I can implement this?
# 4  
Old 08-01-2008
So you have tried nothing so far on your own?
# 5  
Old 08-01-2008
Not much I guess - here is a starting script but I will not write it to the end for you Smilie

Code:
#!/usr/bin/ksh

CUR_DATE=`date +%d/%b/%Y`

if [[ -z $2 ]]; then
        END_DATE=${CUR_DATE}
else
        END_DATE=$2
fi

for MY_ARG in $@; do
        case ${MY_ARG} in
                [0-9][0-9]/[a-zA-Z][a-zA-Z][a-zA-Z]/[0-9][0-9][0-9][0-9])       echo "ok";;
                *)                                                              echo "not ok";;
        esac
done

exit 0

# 6  
Old 08-01-2008
As zaxxon already implied, you should show some effort to solve the problem yourself. We liek to help, but we dislike doing others works.

Having said this, it can be difficult to plan such a task, so here is, how you should work out your solution:

1) we observe, that a "date" is a structured entity: we expect one or two digits first, than a slash, then a string out of a fixed number of strings (the month), again a slash and then another four-digit number.

2) lets first check the simplest parts: are 2 slashes in the input? are they separating 3 strings? Are all the characters out of the set of characters we expect? (For instance, we expect only digits and the letters contained in month names. If the imput contains an "X" or a "?" we can conclude its illegal without further investigation. If the input looks like "...//..." (no 3 strings) we can conclude the same. All these checks can be done via simple sed scripts.

3) If the input passed this test(s), we can break it up into its parts and analyze them separately: a day number of "32" is certainly wrong, "31" might be right depending on the month and "29" (for February) might be wrong depending on the year.

4) When checking the month part you might want to translate "08" to "Aug" instead of rejecting it, the work is the same and it broadens the "input tolerance" of your script.

I think you got the drift now, so start with zaxxon has given to you (which is an excellent starting point, btw.) and work through these suggestions, then come back when you still need help. We'll be glad to support you with some details.

I hope this helps.

bakunin
# 7  
Old 08-01-2008
Also note that this is a question which has been asked before; have you tried searching the forums? At the bottom of this page is a listing of similar threads (though it's apparently mainly based on subject lines, so not always very useful).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Validating date in yyyymmdd format using PERL

Hi all, i had a code where in user will enter a date in yyyymmdd format.. i didnt use any validation for the date and now the problem is if a user enters date instead of month after year it is proceeding with the code.. like if the date is 20120426 and if the user enters 20122604 it... (4 Replies)
Discussion started by: smarty86
4 Replies

2. Shell Programming and Scripting

Check input date format?

how to check input date format. for example $input_date must be in format dd.mm.gg script is execute like this: bin/script1.sh 14.12.2009 script1.sh code: #!/bin/sh input_date=$1 CMD="/app/si/test/test.sh $input_date" echo "*****" $CMD (2 Replies)
Discussion started by: waso
2 Replies

3. UNIX for Dummies Questions & Answers

Validating user input

I'm trying to set up a script that takes user input and validates that the user input was entered correctly. So far I have this: while : do echo "Please enter your name." read NAME if then echo "You have not entered a name." echo... (13 Replies)
Discussion started by: fufaso
13 Replies

4. Shell Programming and Scripting

Validating Input parameters

Hi All, I have tried to use ckdate (sun) command in script. It checks the input parameter which should be in 'YYYYMMDD format. date=$( echo $1 | ckdate -f "%Y%m%d") | true if ] then print " success" else print "no success" fi But in whatever format i pass the parameter,... (3 Replies)
Discussion started by: Amit.Sagpariya
3 Replies

5. UNIX for Dummies Questions & Answers

Validating input based on fixed number of fields

Yes, i did... let me state my problem in more detail Inputs: I have one input CSV file And, i have stored no. of comma each line should in a variable. e.g. $ cat cmt.csv this, is a ,comma ,count test1 ,,this, is a ,comma ,count test2 this, is a ,comma ,count test3... (6 Replies)
Discussion started by: Dipali
6 Replies

6. Shell Programming and Scripting

Need to validate a date input format

Hi all, I have a shell script(K shell) which takes a date as input. i want the input to be in DD-MM-YYYY format. Can i enforce such a format of input string using just one line of code? OR do i need to parse the input date into different components and test them using Case statements... (2 Replies)
Discussion started by: rajugp1
2 Replies

7. Shell Programming and Scripting

validating a input file for numeric and character

i have a input file like this 001|rahim|bajaj|20090102 while reading the file i need to check whether the first column is a number second column is a name is there any methodology to check for the same thanks in advance (2 Replies)
Discussion started by: trichyselva
2 Replies

8. Shell Programming and Scripting

Validating user input is not blank

Trying to create a script in BASH that would ask the user to enter another user name making sure the input is not blank before they hit enter then to check the home directory of that user does exist, I have the check folder sorted it's just the loop to make sure the user has entered chars (5 Replies)
Discussion started by: MBN
5 Replies

9. Shell Programming and Scripting

validating input using regular expressions

I am trying to validate input from the user in a script. I thought is was easy to do using regular expressions but I can't figure out how to use REs in a conditional. I have tried using grep from a temp file, sed from a temp file, sed from command line, comparison in an if condition and I cannot... (1 Reply)
Discussion started by: nrodolfich
1 Replies

10. Programming

validating input

how do i validate y script so that it only accepts values between 1 and 3 and against any character input, cause at the moment i can only validate against numbers outside 1 and 3 but not characters cheers (4 Replies)
Discussion started by: ruffenator
4 Replies
Login or Register to Ask a Question