|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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 |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
||||
|
||||
|
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 |
|
#5
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Python / CGI question. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|