-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormFieldEventsExample01.htm
executable file
·55 lines (46 loc) · 1.95 KB
/
FormFieldEventsExample01.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<!DOCTYPE html>
<html>
<head>
<title>Form Field Events Example</title>
<script type="text/javascript" src="EventUtil.js"></script>
</head>
<body>
<p>The textbox will receive focus as soon as the page loads. You can then start typing.</p>
<form method="post" action="javascript:alert('Form submitted!')" id="myForm">
<div>
<label for="comments">Type numbers:</label><br />
<input type="text" id="txtNumbers" name="numbers" />
</div>
<input type="submit" value="Submit Form" id="submit-btn" />
</form>
<script type="text/javascript">
EventUtil.addHandler(window, "load", function(event){
var textbox = document.forms[0].elements[0];
EventUtil.addHandler(textbox, "focus", function(event){
event = EventUtil.getEvent(event);
var target = EventUtil.getTarget(event);
target.style.backgroundColor = "yellow";
});
EventUtil.addHandler(textbox, "blur", function(event){
event = EventUtil.getEvent(event);
var target = EventUtil.getTarget(event);
if (/[^\d]/.test(target.value)){
target.style.backgroundColor = "red";
} else {
target.style.backgroundColor = "";
}
});
EventUtil.addHandler(textbox, "change", function(event){
event = EventUtil.getEvent(event);
var target = EventUtil.getTarget(event);
if (/[^\d]/.test(target.value)){
target.style.backgroundColor = "red";
} else {
target.style.backgroundColor = "";
}
});
textbox.focus();
});
</script>
</body>
</html>