469,916 Members | 2,523 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,916 developers. It's quick & easy.

scanning for numerals / letters

Kun
I have the following if statement that checks if a form is empty:

if form.has_key("date") and form["date"].value != "":
date=form['date'].value

else:
print "ERROR: No date entered!"
raise Exception

I would also like to add another if statement checking if 'date' has any
letters (a-z) in it, and if so, would like to say that "you have to
enter a date with numbers". I am not sure how to alter my current if
statement to do that check so any assistance would be appreciated.
On the flip side, I also have a field called 'purchases' where the user
must enter non-numerals, thus i would also like to know how to scan to
see if their entry has numerals and print 'please do not use numbers' if
they did.

Thanks for your help.
Apr 19 '06 #1
4 1057
Something like this should work:

==================
for c in form.get('date'):
if c in string.letters:
print "ERROR: You have to enter a date with numbers."
==================

You have to import the string module. 'letters' is one of the
attributes defined in that module.

Other attributes defined there include 'digits'. (Hint, hint. ;-) )

Also note that if you are not sure if a dictionary has a particular
key, you can use:

form.get('date')

and you will get the value if the key 'date' exists. If not, you get
None.

It's handier than checking has_key.

You can also say:

form.get('date', '01/01/70')

If the dictionary has a key 'date', you will get the value associated
with it. Othewise, you will get '01/01/70'.

Apr 19 '06 #2
What about this?

import re
if not form.get('date'):
print "Tsk! No date entered."
raise Exception

if re.search('[a-zA-Z]', form.get('date')):
print "Tsk! No fancy date words."
raise Exception

date = form.get('date')

if not form.get('purchases'):
print "Tsk! Are you buying or not?"
raise Exception

if re.search('[0-9]', form.get('purchases')):
print "Tsk! For some reason, you can't buy anything with numbers in it."
raise Exception

purchases = form.get('purchases')
Kun wrote:
I have the following if statement that checks if a form is empty:

if form.has_key("date") and form["date"].value != "":
date=form['date'].value

else:
print "ERROR: No date entered!"
raise Exception

I would also like to add another if statement checking if 'date' has any
letters (a-z) in it, and if so, would like to say that "you have to
enter a date with numbers". I am not sure how to alter my current if
statement to do that check so any assistance would be appreciated.
On the flip side, I also have a field called 'purchases' where the user
must enter non-numerals, thus i would also like to know how to scan to
see if their entry has numerals and print 'please do not use numbers' if
they did.

Thanks for your help.


--
Dale Strickland-Clark
Riverhall Systems - www.riverhall.co.uk
We're recruiting programmers. Please see the web site.

Apr 19 '06 #3
First, note that form["date"] is all you need. form["date"].value is
redundant.

I would do this with sets:

import string
if set(form["date"]) & set(string.ascii_letters) != set([]): print "You
have to enter a date with numbers"

if set(form["purchases"]) & set(string.digits) != set([]): print
"Please do not use numbers"

Sets take time to construct, but they test membership faster than
strings. Plus, the code just reads logically. (The &, if you couldn't
figure it out, does an intersection of sets. You could also use the
..intersection method for even better readability).

Apr 19 '06 #4
Kun wrote:
I have the following if statement that checks if a form is empty:

if form.has_key("date") and form["date"].value != "":
date=form['date'].value

else:
print "ERROR: No date entered!"
raise Exception

I would also like to add another if statement checking if 'date' has any
letters (a-z) in it, and if so, would like to say that "you have to
enter a date with numbers". I am not sure how to alter my current if
statement to do that check so any assistance would be appreciated.


Having just attempted a 'string_to_date' function I can see the wisdom
of having separate 'day', 'month' and 'year' input fields on the
client. If you can't or won't provide separate fields then I suppose
you have to inform users as to what you accept as valid input, eg.
'ddmmyy', or 'month/day/year'. Here's some code which assumes that you
are providing appropriate formatting hints:

import time
import datetime

DDMMYY = ['%d %m %Y', '%d %m %y', '%d/%m/%Y', '%d/%m/%y', '%d-%m-%Y',
'%d-%m-%y' ]

def yearmonthday(datestring, fmts=DDMMYY):
ymd = tuple()
for f in fmts:
try:
ymd = time.strptime( datestring, f )
break
except ValueError:
continue
if not ymd:
raise ValueError
return ymd[0], ymd[1], ymd[2]

def is_valid_date(datestring, fmts=DDMMYY):
try:
yearmonthday(datestring, fmts)
return True
except ValueError:
return False

def string_to_date(datestring, fmts=DDMMYY):
return datetime.date( *yearmonthday(datestring, fmts) )

assert string_to_date( '1/2/01', DDMMYY) == datetime.date(2001,2,1)
assert string_to_date( '1 2 01', DDMMYY) == datetime.date(2001,2,1)
assert string_to_date( '01/02/01', DDMMYY) == datetime.date(2001,2,1)
assert string_to_date( '1/02/2001', DDMMYY) == datetime.date(2001,2,1)
assert string_to_date( '29/02/2008', DDMMYY) ==
datetime.date(2008,2,29)
assert string_to_date( '01/2/99', DDMMYY) == datetime.date(1999,2,1)

for d in [ '', '32/1/01', '01/13/01', '29/2/07', '1/2', 'abcdef' ]:
assert not is_valid_date(d, DDMMYY)
Gerard

Apr 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Mutley | last post: by
13 posts views Thread by Christopher Benson-Manica | last post: by
4 posts views Thread by Patrick Blackman | last post: by
5 posts views Thread by Kamal R. Prasad | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.