Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

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:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old May 9th, 2003, 01:22 PM
CaitlinG CaitlinG is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 9 CaitlinG User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old May 9th, 2003, 05:55 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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

Reply With Quote
  #3  
Old May 10th, 2003, 01:11 PM
CaitlinG CaitlinG is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 9 CaitlinG User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old May 11th, 2003, 05:51 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Talking 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

Reply With Quote
  #5  
Old May 11th, 2003, 06:40 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Talking 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.
Attached Files
File Type: py form.py (639 Bytes, 364 views)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Python / CGI question.


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 1 hosted by Hostway