~ubuntu-branches/debian/squeeze/alpine/squeeze

« back to all changes in this revision

Viewing changes to web/src/cgi.tcl-1.10/example/validate.cgi

  • Committer: Bazaar Package Importer
  • Author(s): Asheesh Laroia
  • Date: 2007-02-17 13:17:42 UTC
  • Revision ID: james.westby@ubuntu.com-20070217131742-99x5c6cpg1pbkdhw
Tags: upstream-0.82+dfsg
ImportĀ upstreamĀ versionĀ 0.82+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/depot/path/tclsh
 
2
 
 
3
# This CGI script uses JavaScript to validate a form before submission.
 
4
 
 
5
package require cgi
 
6
 
 
7
cgi_eval {
 
8
    source example.tcl
 
9
 
 
10
    cgi_input
 
11
 
 
12
    cgi_head {
 
13
        title "Using JavaScript to validate a form before submission"
 
14
 
 
15
        javascript {
 
16
            puts {
 
17
                function odd(num) {
 
18
                    if (num.value % 2 == 0) {
 
19
                        alert("Please enter an odd number!")
 
20
                        num.value = ""
 
21
                        return false
 
22
                    }
 
23
                    return true
 
24
                }
 
25
            }
 
26
        }
 
27
        noscript {
 
28
            puts "Sorry - your browser doesn't understand JavaScript."
 
29
        }
 
30
    }
 
31
 
 
32
    set rownum 0
 
33
    proc row {msg {event {}}} {
 
34
        global rownum
 
35
 
 
36
        incr rownum
 
37
        table_row {
 
38
            table_data nowrap {
 
39
                put  "Odd number: "
 
40
                text num$rownum= size=4 $event
 
41
            }
 
42
            table_data {
 
43
                puts $msg
 
44
            }
 
45
        }
 
46
    }
 
47
 
 
48
    cgi_body {
 
49
        set more ""
 
50
        if {0 == [catch {import num3}]} {
 
51
            set count [scan $num3 %d num]
 
52
            if {($count != 1) || ($num % 2 == 0)} {
 
53
                p "Hey, you didn't enter an odd number!"
 
54
            } else {
 
55
                p "Thanks for entering odd numbers!"
 
56
                set more " more"
 
57
            }
 
58
        }
 
59
 
 
60
        puts "Please enter$more odd numbers - thanks!"
 
61
 
 
62
        cgi_form validate "onSubmit=return odd(this.num2)" {
 
63
            table {
 
64
                row "This number will be validated when it is entered." onChange=odd(this.form.num1)
 
65
                row "This number will be validated when the form is submitted."
 
66
                row "This number will be validated after the form is submitted."
 
67
            }
 
68
            submit_button =Submit
 
69
        }
 
70
 
 
71
        h5 "Note: JavaScript validation should always be accompanied
 
72
        by validation in the backend (CGI script) since browsers
 
73
        cannot be relied upon to have JavaScript enabled (or supported
 
74
        in the first place).  Sigh."
 
75
    }
 
76
}