1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < title > Chapter 16: Exercise 2</ title >
5+ < style >
6+ .fieldname {
7+ text-align : right;
8+ }
9+ .submit {
10+ text-align : right;
11+ }
12+ </ style >
13+ </ head >
14+ < body >
15+ < form >
16+ < table >
17+ < tr >
18+ < td class ="fieldname "> Username:</ td >
19+ < td > < input type ="text " id ="username " /> </ td >
20+ < td > < a id ="usernameAvailability " href ="# "> Check Availability</ a > </ td >
21+ </ tr >
22+ < tr >
23+ < td class ="fieldname "> Email:</ td >
24+ < td > < input type ="text " id ="email " /> </ td >
25+ < td > < a id ="emailAvailability " href ="# "> Check Availability</ a > </ td >
26+ </ tr >
27+ < tr >
28+ < td class ="fieldname "> Password:</ td >
29+ < td > < input type ="text " id ="password " /> </ td >
30+ < td />
31+ </ tr >
32+ < tr >
33+ < td class ="fieldname "> Verify Password:</ td >
34+ < td > < input type ="text " id ="password2 " /> </ td >
35+ < td />
36+ </ tr >
37+ < tr >
38+ < td colspan ="2 " class ="submit "> < input type ="submit " value ="Submit " /> </ td >
39+ < td />
40+ </ tr >
41+ </ table >
42+ </ form >
43+ < script src ="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js "> </ script >
44+ < script >
45+ function check ( e ) {
46+ e . preventDefault ( ) ;
47+ var userValue = $ ( "#username" ) . val ( ) ;
48+ var emailValue = $ ( "#email" ) . val ( ) ;
49+ var parms = { }
50+ if ( e . target . id == "usernameAvailability" ) {
51+ if ( ! userValue ) {
52+ alert ( "Please enter a user name to check!" ) ;
53+ return ;
54+ }
55+ parms = {
56+ username : userValue
57+ } ;
58+ } else {
59+ if ( ! emailValue ) {
60+ alert ( "Please enter an email address to check!" ) ;
61+ return ;
62+ }
63+ parms = {
64+ email : emailValue
65+ } ;
66+ }
67+
68+ $ . getJSON ( "ch14_formvalidator.php" , parms ) . done ( handleResponse ) . fail ( failure ) ;
69+ }
70+ function handleResponse ( response ) {
71+ if ( response . available ) {
72+ alert ( response . searchTerm + " is available!" ) ;
73+ } else {
74+ alert ( "We're sorry, but " + response . searchTerm + " is not available." ) ;
75+ }
76+ }
77+ function failure ( ) {
78+ alert ( "Sorry, an error has occured." ) ;
79+ }
80+ $ ( "#usernameAvailability" ) . on ( "click" , check ) ;
81+ $ ( "#emailAvailability" ) . on ( "click" , check ) ;
82+ </ script >
83+ </ body >
84+ </ html >
0 commit comments