Except where otherwise noted, the contents of this presentation are © Copyright 2007 Marty Stepp and are licensed under the Creative Commons Attribution 2.5 License.
<form>
<form action="web app URL" method="get or post"> <fieldset> form controls </fieldset> </form>
action
attribute gives the URL of the server web application that will process this form's datamethod
attribute specifies whether the server should use an HTTP GET or POST command (explained later)<form action="http://www.foo.com/app.php" method="get"> <fieldset> <label>Name: <input type="text" name="name" /></label> <label>Meal: <input type="text" name="meal" /></label> <label>Meat? <input type="checkbox" name="meat" /> </label> <input type="submit" /> <fieldset> </form>
fieldset
sname
attribute<form action="http://foo.com/app.php" method="get"> <fieldset> <label>Name: <input type="text" name="name" /></label> <label>Meal: <input type="text" name="meal" /></label> <label>Meat? <input type="checkbox" name="meat" /> </label> <input type="submit" /> <fieldset> </form>
name
specifies the query string parameter to passhttp://foo.com/app.php?name=Sue&meal=pizza&meat=on
get
vs. post
<form action="http://www.foo.com/app.php" method="post">
...
</form>
get
request passes the parameters to the server as a query stringpost
request embeds the parameters in HTTP request, not in the URLpost
:
get
is limited to browser's URL length, around 100-200 charactersget
(click "Buy it!")post
:
submit
and reset
buttons<form action="http://www.foo.com/app.php" method="get"> <fieldset> ... <input type="submit" /> <input type="reset" /> <fieldset> </form>
input
element with a submit
attribute is displayed as a button that, when clicked, will send the parameters to the server and show the responseinput
element with a reset
attribute is displayed as a button that, when clicked, will change the controls back to their original statesubmit
, reset
examplevalue
attribute
<input type="submit" value="Order Meal" />
element[attribute="value"] { ... }
input[type="text"] {
color: blue;
font-style: italic;
margin-bottom: 2em;
}
input
element represents many different controlsCreate a web page with a form that lets the user sign up for an online banking account such as those offered at Washington Mutual or Orange Savings.
// in window.onload handler: var submit = document.getElementById("submitbutton"); submit.onclick = submitClick; ... function submitClick(event) { if (document.getElementById("name").value == "") { event.preventDefault(); // cancel submit } }
preventDefault
to tell the button to stop its default behaviorreturn false;
instead of preventDefault
/^[\w\.%\-]+@[\w.\-]+\.[a-zA-Z]{2,4}$/
Scanner
's useDelimiter
method, Pattern
class, String
's split
method (CSE 143 sentence generator)/abc/
/
"abcdef"
, "defabc"
, ".=.abc.=."
, ..."fedcba"
, "ab c"
, "JavaScript"
, ....
matches any character except newline
/.oo.y/
matches "Doocy", "goofy", "PooPy", ...|
means or
/abc|def|g/
matches "abc", "def", or "g"()
are for grouping
/(Homer|Marge) S/
matches "Homer S" or "Marge S"/Homer|Marge|Bart|Lisa|Maggie S/
match?^
means beginning of line; $
means end
/^<html>$/
matches a line that consists entirely of "<html>"
*
means 0 or more occurrences
/abc*/
matches "ab", "abc", "abcc", .../a(bc)*/
matches "a", "abc", "abcbc", "abcbcbc", .../a.*a/
matches "aa", "aba", "a8qa", "a!?_a", ...+
means 1 or more occurrences
/a(bc)+/
matches "abc", "abcbc", "abcabcabc", .../Goo+gle/
matches "Google", "Gooogle", "Goooogle", ...?
means 0 or 1 occurrences
/a(bc)?/
matches "a" or "abc"{min,max}
means between min and max occurrences (inclusive)
/a(bc){2, 4}/
matches "abcbc", "abcbcbc", or "abcbcbcbc"{2,}
means 2 or more{,6}
means up to 6{3}
exactly 3[]
group characters into a character set; the expression will match any single character from the set
/[bcd]art/
matches "bart", "cart", and "dart"[]
, many of the modifier keys act as normal characters
/what[!*?]*/
matches "what", "what!", "what?**!", "what??!", ...-
/[a-z]/
matches any lowercase letter/[a-zA-Z0-9]/
matches any lower- or uppercase letter or digit^
means negation
/[^abcd]/
matches any character other than a, b, c, or d\
starts an escape sequence\d
matches any digit (same as [0-9]
)\D
matches any non-digit (same as [^0-9]
)\w
matches any "word character" (same as [a-zA-Z_0-9]
)\W
matches any non-word character (same as [^a-zA-Z_0-9]
)\s
matches any whitespace ( , \t
, \n
, etc.)\S
matches any non-whitespace\
: \ | ( ) [ { ^ $ * + ? .
/\\|\/\?/
matches "\|/?"
-
must be escaped to be matched
/[0-9+\-]/
matches any single digit, a +
, or a -
string.match(regexp)
- if string fits the pattern, returns matching text; else returns
null
- can be used in Boolean context
if (myString.match(/[a-z]+/)) { ... }
i
can be placed after the regexp for a case-insensitive match
myString.match(/Marty/i)
will match "marty", "MaRtY", ...var regexp = /(Bart|Lisa|Maggie) Simpson/;
if (myString.match(regexp)) { ... }
string.replace(regexp, "text")
- replaces the first occurrence of given pattern with the given text
var str = "Marty Stepp";
str.replace(/[a-z]/, "x")
returns "Mxrty Stepp"
- returns the modified string as its result; must be stored
str = str.replace(/[a-z]/, "x")
g
can be placed after the regexp for a global match (replace all occurrences)
str.replace(/[a-z]/g, "x")
returns "Mxxxx Sxxxx"
str = str.replace(/[^A-Z]+/g, "")
turns str
into "MS"
Use regular expressions to validate the online banking form you wrote previously.
alert
if the data is bad, and not submitting the form.