UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old December 14th, 2004, 10:58 PM
jaxi_aj jaxi_aj is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 4 jaxi_aj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 52 sec
Reputation Power: 0
Question Number & Date Validation Query

Hi all,
Do you have any pointers how to validate numbers (not to contain alphabets and special characters) and date(MM/DD/YYYY) format.

I used following regular expression to validate integer, which is not working in the default shell:

nodigits="$(echo $testvalue | sed 's/[[:digit:]]//g')"

if [ ! -z $nodigits ] ; then
echo "Invalid number format! Only digits, no commas, spaces, etc." >&2
return 1
fi


Alok

Reply With Quote
  #2  
Old December 15th, 2004, 06:32 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,077 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 13 h 40 m 11 sec
Reputation Power: 8
why not simply use (old and) powerfull tools ?

case x$input in x[0-9]*[0-9]) ;; *) print an error mssg and exit ;; esac
do the rest
exit 0

NOTA: this works w/ ALL shells (only csh needs switch instead of case, but csh
never was a good scripting shell, so don't use csh for scripts)

Reply With Quote
  #3  
Old December 15th, 2004, 03:25 PM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,307 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 4 h 28 m 57 sec
Reputation Power: 48
regular expressions don't do much for dates. You can either write a simple C module
that calls getdate() which does validate dates, or use another module that already does it. For example, touch validates dates.

Here is a primitive example that validates a full date and time in YYYYMMDDhhmmss format.

Code:
#!/bin/ksh
# validate date/time
MMDD=$(expr substr "$*" 1 4)
YYYY=$(expr substr "$*" 5 4)
hh=$(expr substr "$*" 10 2)
mm=$(expr substr "$*" 13 2)
ss=$(expr substr "$*" 16 2)
if touch -c -t $YYYY$MMDD$hh$mm.$ss dummyfile
then
   echo valid
else
   echo invalid

fi

Reply With Quote
  #4  
Old December 16th, 2004, 08:49 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 116 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 34 sec
Reputation Power: 6
Try this to check the date string:

:
[ $# -eq 1 ] || {
echo "$0: usage: $0 MM/DD/YYYY" >&2
exit 2
}

case $1 in
[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]) : ;;
*) echo invalid
exit 1 ;;
esac

IFS=/ # redefine internal field separators
set $1 # separate the fields into $1, $2, $3

# touch does not check for 31st of February.
# If necessary, write a better check
if touch -ct $3$2$10000 x
then
echo valid
else
echo invalid
exit 1
fi


Regads

Reply With Quote
  #5  
Old December 16th, 2004, 09:10 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 116 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 34 sec
Reputation Power: 6
The test for integer from guggach

"
case x$input in x[0-9]*[0-9]) ;; *) print an error mssg and exit ;; esac
do the rest
exit 0
"

is not OK, because it evaluates "1XXX2" as valid.

In Korn shell you can use:

case $1 in
+([0-9])) echo valid ;;
*) echo invalid ;;
esac

In Bourne shell you can count all characters and all digits. If the counts are equal, the input is an integer:

C=`expr $1 : ".*"`
D=`expr $1 : "[0-9][0-9]*"`
[ $C -eq $D ] && {
echo valid
}

Regards

Reply With Quote
  #6  
Old January 12th, 2005, 02:53 PM
amadensor amadensor is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 1 amadensor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
A slick one liner

None of those methods does much for validating that the date is a valid date. Feb 32 is validated as Mar 3. I took it and used cal, sed , and grep to validate a recovery date in CCYY/MM/DD:

#Validate that the cycle date is a real date
MM=$(expr substr "$recovery" 6 2)
DD=$(expr substr "$recovery" 9 4)
YYYY=$(expr substr "$recovery" 1 4)
D=`echo $DD|sed s/^0//`
cal $MM $YYYY|tail +3|grep $D>/dev/null
if [ ${?} -ne 0 ]; then
echo "ERROR: Invalid recovery date. Process end."
exit 1
fi


The heart of this is a one-liner:
cal $MM $YYYY|tail +3|grep $D>/dev/null

Everything else is window dressing.

Reply With Quote
  #7  
Old January 12th, 2005, 09:31 PM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,077 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 13 h 40 m 11 sec
Reputation Power: 8
so so

Quote:
The test for integer from guggach

"
case x$input in x[0-9]*[0-9]) ;; *) print an error mssg and exit ;; esac
do the rest
exit 0
"

is not OK, because it evaluates "1XXX2" as valid.


prouve it

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Number & Date Validation Query


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway