Ajax and Jquery: Jeffrey Miller, PH.D
Ajax and Jquery: Jeffrey Miller, PH.D
Ajax and Jquery: Jeffrey Miller, PH.D
CSCI 201
Principles of Software Development
› You need to include a .js file in your code, and then jQuery can be used
› http://jquery.com
10/14
jQuery/Servlet Form Validation
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>AJAX Form</title>
5 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
6 <script>
7 function validateUsername() {
8 $.ajax({
9 url: "Validate",
10 data: {
11 username: document.myform.username.value,
12 field: "username"
13 },
14 success: function( result ) {
15 $( "#usernameMsg").html(result);
16 }
17 });
18 }
19 function validatePassword() {
20 $.ajax({
21 url: "Validate",
22 data: {
23 password: document.myform.pass.value,
24 field: "password"
25 },
26 success: function( result ) {
27 $( "#passwordMsg").html(result);
28 }
29 });
30 }
31 </script>
32 </head>
33 <body>
34 <form name="myform" action="SubmitServlet" method="POST">
35 Username <input type="text" name="username" value="jeff" onblur="validateUsername();"/>
36 <strong><div id="usernameMsg"></div></strong>
37 <br />
38 Password <input type="password" name="pass" value="test" onblur="validatePassword();"/>
39 <strong><div id="passwordMsg"></div></strong>
40 <br />
41 <input type="submit" name="submit" value="Authenticate" />
42 </form>
43 </body>
44 </html>
12/14
Outline
• AJAX
• Program