Lecture 18
More Events and Validation
Reading: 9.2 - 9.3
Except where otherwise noted, the contents of this presentation are Copyright 2009 Marty Stepp and Jessica Miller.
Page/window events
(9.2.5)
name | description |
load
|
the browser loads the page
|
unload
|
the browser exits the page
|
resize
|
the browser window is resized
|
contextmenu
|
the user right-clicks to pop up a context menu
|
error
|
an error occurs when loading a document or an image
|
-
The above events can be handled on the global
window
object. Also:
window.onload = function() { ... };
document.observe("dom:loaded", function() {
$("orderform").observe("submit", verify);
});
Form events
(9.2.4)
event name
|
description
|
submit
|
form is being submitted
|
reset
|
form is being reset
|
change
|
the text or state of a form control has changed
|
window.observe("load", function() {
$("orderform").observe("submit", verify);
});
function verify(event) {
if ($("zipcode").value.length < 5) {
event.stop();
}
}
Prototype and forms
(9.1.6)
$("id")["name"]
-
gets parameter with given name from form with given id
$F("id")
-
other form control methods:
activate |
clear |
disable |
enable |
focus |
getValue |
present |
select |
Client-side validation code
<form id="exampleform" action="http://foo.com/foo.php">
window.onload = function() {
$("exampleform").onsubmit = checkData;
};
function checkData(event) {
if ($("city").value == "" || $("state").value.length != 2) {
Event.stop(event);
alert("Error, invalid city/state.");
}
}
- forms expose
onsubmit
and onreset
events
- to abort a form submission, call Prototype's
Event.stop
on the event
Regular expressions in JavaScript
string.match(regex)
- if string fits the pattern, returns the matching text; else returns
null
- can be used as a Boolean truthy/falsey test:
var name = $("name").value;
if (name.match(/[a-z]+/)) { ... }
- an
i
can be placed after the regex for a case-insensitive match
name.match(/Marty/i)
will match "marty"
, "MaRtY"
, ...
Replacing text with regular expressions
string.replace(regex, "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")
- a
g
can be placed after the regex for a global match (replace all occurrences)
str.replace(/[a-z]/g, "x")
returns "Mxxxx Sxxxx"
- using a regex as a filter
str = str.replace(/[^A-Z]+/g, "")
turns str
into "MS"
Keyboard/text events
(9.2.3)
name |
description |
keydown
|
user presses a key while this element has keyboard focus
|
keyup
|
user releases a key while this element has keyboard focus
|
keypress
|
user presses and releases a key while this element has keyboard focus
|
focus
|
this element gains keyboard focus
|
blur
|
this element loses keyboard focus
|
select
|
this element's text is selected or deselected)
|
-
focus: the attention of the user's keyboard (given to one element at a time)
Key event objects
property name
|
description
|
keyCode
|
ASCII integer value of key that was pressed
(convert to char with String.fromCharCode )
|
altKey , ctrlKey , shiftKey
|
true if Alt/Ctrl/Shift key is being held
|
Prototype's key code constants
Event.KEY_BACKSPACE
|
Event.KEY_DELETE
|
Event.KEY_DOWN
|
Event.KEY_END
|
Event.KEY_ESC
|
Event.KEY_HOME
|
Event.KEY_LEFT
|
Event.KEY_PAGEDOWN
|
Event.KEY_PAGEUP
|
Event.KEY_RETURN
|
Event.KEY_RIGHT
|
Event.KEY_TAB
|
Event.KEY_UP
|
|
|
|
-
issue: if the event you attach your listener to doesn't have the focus, you won't hear the event
-
possible solution: attach key listener to entire page body, outer element, etc.