#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Posts
    9
    Rep 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
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69

    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
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Posts
    9
    Rep 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.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69

    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
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69

    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

IMN logo majestic logo threadwatch logo seochat tools logo