The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Python / CGI question.
Discuss Python / CGI question. in the Python Programming forum on Dev Shed. Python / CGI question. Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 9th, 2003, 01:22 PM
|
|
Junior Member
|
|
Join Date: Apr 2002
Posts: 9
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Python / CGI question.
Hi.
I am writing a form validator and in an attempt to 'streamline' my code, I would like to iterate over the form fields with one loop. Here is my code so far:
#!c:\python22\python.exe
# Import three python modules.
import cgi, re, string
# Python function to send header info to web server.
# This is and the blank line under it are required.
def printHeader( title ):
print """Content-type: text/html
<html><head><title>%s</title></head>
<body>""" % title
# Call the printHeader function and pass it the quoted
# string.
printHeader( "Python Form Validator" )
# Create an object of cgi.FieldStorage and refer to it as
# 'form'.
form = cgi.FieldStorage()
# Create the individual regex patterns to compare
# the field data to.
fnamePattern = "[^a-zA-Z]+"
lnamePattern = "[^a-zA-Z]+"
emailPattern = "\w+\s+\w+@\.\w{2,4}"
telephonePattern = "^\d{3}(.-)\d{3}(.-)\d{4}$"
majorPattern = "[^a-zA-Z]+"
cityPattern = "[^a-zA-Z]+"
statePattern = "[^a-zA-Z]+"
zipPattern = "^\d\d\d\d\d$"
data = []
data.append ( add each form field )
for i in data:
if the field != None and it is checked for content by the specific regex above, then fine. Otherwise, print the 'field name' contains missing or invalid data.
print "</body></html>"
Thanks in advance for any help.
-Caitlin
|

May 9th, 2003, 05:55 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
Form validation
I'm not sure exactly why you wanted to loop but heres a simple example of form validation -
form = cgi.FieldStorage()
if re.search(form['field'].value, expression): print 'yes, all right'
else: print 'Oops, one more try'
That's basically how it works, that what you ment?
Mark
|

May 10th, 2003, 01:11 PM
|
|
Junior Member
|
|
Join Date: Apr 2002
Posts: 9
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Form validaton.
Hi Mark.
Thanks. That's close. If I had 5 form fields:
name, age, phone, major, state
I would like to store them in a list, then instead of 5 seperate if / else blocks ( messy ), I thought one for loop that looks at each element in the list and evaluates it for either 'blankness' or a regex failure would be more elegant. When I tried that, the python script simply display a blank screen.
Any ideas?
Thanks,
Caitlin.
|

May 11th, 2003, 05:51 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Mmmm
Ok, well if you give me a list of the regular expressions that you want to use to compare which form fields then i'll look into it. Got a good idea of how to do it.
Mark
|

May 11th, 2003, 06:40 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
For loop validation
Hey again Caitlin, I managed to solve the whole problem. I've attached the code. There's lots of room for improvment.
Hope this is what you wanted.
Have fun,
Mark.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|