1 |
<!doctype html> |
2 |
<html lang="en"> |
3 |
<head> |
4 |
<meta charset="utf-8"> |
5 |
<title>jQuery UI Dialog - Modal form</title> |
6 |
<link rel="stylesheet" href="../../themes/base/jquery.ui.all.css"> |
7 |
<script src="../../jquery-1.9.1.js"></script> |
8 |
<script src="../../ui/jquery.ui.core.js"></script> |
9 |
<script src="../../ui/jquery.ui.widget.js"></script> |
10 |
<script src="../../ui/jquery.ui.mouse.js"></script> |
11 |
<script src="../../ui/jquery.ui.button.js"></script> |
12 |
<script src="../../ui/jquery.ui.draggable.js"></script> |
13 |
<script src="../../ui/jquery.ui.position.js"></script> |
14 |
<script src="../../ui/jquery.ui.resizable.js"></script> |
15 |
<script src="../../ui/jquery.ui.button.js"></script> |
16 |
<script src="../../ui/jquery.ui.dialog.js"></script> |
17 |
<script src="../../ui/jquery.ui.effect.js"></script> |
18 |
<link rel="stylesheet" href="../demos.css"> |
19 |
<style> |
20 |
body { font-size: 62.5%; } |
21 |
label, input { display:block; } |
22 |
input.text { margin-bottom:12px; width:95%; padding: .4em; } |
23 |
fieldset { padding:0; border:0; margin-top:25px; } |
24 |
h1 { font-size: 1.2em; margin: .6em 0; } |
25 |
div#users-contain { width: 350px; margin: 20px 0; } |
26 |
div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; } |
27 |
div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; } |
28 |
.ui-dialog .ui-state-error { padding: .3em; } |
29 |
.validateTips { border: 1px solid transparent; padding: 0.3em; } |
30 |
</style> |
31 |
<script> |
32 |
$(function() { |
33 |
var name = $( "#name" ), |
34 |
email = $( "#email" ), |
35 |
password = $( "#password" ), |
36 |
allFields = $( [] ).add( name ).add( email ).add( password ), |
37 |
tips = $( ".validateTips" ); |
38 |
|
39 |
function updateTips( t ) { |
40 |
tips |
41 |
.text( t ) |
42 |
.addClass( "ui-state-highlight" ); |
43 |
setTimeout(function() { |
44 |
tips.removeClass( "ui-state-highlight", 1500 ); |
45 |
}, 500 ); |
46 |
} |
47 |
|
48 |
function checkLength( o, n, min, max ) { |
49 |
if ( o.val().length > max || o.val().length < min ) { |
50 |
o.addClass( "ui-state-error" ); |
51 |
updateTips( "Length of " + n + " must be between " + |
52 |
min + " and " + max + "." ); |
53 |
return false; |
54 |
} else { |
55 |
return true; |
56 |
} |
57 |
} |
58 |
|
59 |
function checkRegexp( o, regexp, n ) { |
60 |
if ( !( regexp.test( o.val() ) ) ) { |
61 |
o.addClass( "ui-state-error" ); |
62 |
updateTips( n ); |
63 |
return false; |
64 |
} else { |
65 |
return true; |
66 |
} |
67 |
} |
68 |
|
69 |
$( "#dialog-form" ).dialog({ |
70 |
autoOpen: false, |
71 |
height: 300, |
72 |
width: 350, |
73 |
modal: true, |
74 |
buttons: { |
75 |
"Create an account": function() { |
76 |
var bValid = true; |
77 |
allFields.removeClass( "ui-state-error" ); |
78 |
|
79 |
bValid = bValid && checkLength( name, "username", 3, 16 ); |
80 |
bValid = bValid && checkLength( email, "email", 6, 80 ); |
81 |
bValid = bValid && checkLength( password, "password", 5, 16 ); |
82 |
|
83 |
bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." ); |
84 |
// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/ |
85 |
bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com" ); |
86 |
bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" ); |
87 |
|
88 |
if ( bValid ) { |
89 |
$( "#users tbody" ).append( "<tr>" + |
90 |
"<td>" + name.val() + "</td>" + |
91 |
"<td>" + email.val() + "</td>" + |
92 |
"<td>" + password.val() + "</td>" + |
93 |
"</tr>" ); |
94 |
$( this ).dialog( "close" ); |
95 |
} |
96 |
}, |
97 |
Cancel: function() { |
98 |
$( this ).dialog( "close" ); |
99 |
} |
100 |
}, |
101 |
close: function() { |
102 |
allFields.val( "" ).removeClass( "ui-state-error" ); |
103 |
} |
104 |
}); |
105 |
|
106 |
$( "#create-user" ) |
107 |
.button() |
108 |
.click(function() { |
109 |
$( "#dialog-form" ).dialog( "open" ); |
110 |
}); |
111 |
}); |
112 |
</script> |
113 |
</head> |
114 |
<body> |
115 |
|
116 |
<div id="dialog-form" title="Create new user"> |
117 |
<p class="validateTips">All form fields are required.</p> |
118 |
|
119 |
<form> |
120 |
<fieldset> |
121 |
<label for="name">Name</label> |
122 |
<input type="text" name="name" id="name" class="text ui-widget-content ui-corner-all" /> |
123 |
<label for="email">Email</label> |
124 |
<input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all" /> |
125 |
<label for="password">Password</label> |
126 |
<input type="password" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" /> |
127 |
</fieldset> |
128 |
</form> |
129 |
</div> |
130 |
|
131 |
|
132 |
<div id="users-contain" class="ui-widget"> |
133 |
<h1>Existing Users:</h1> |
134 |
<table id="users" class="ui-widget ui-widget-content"> |
135 |
<thead> |
136 |
<tr class="ui-widget-header "> |
137 |
<th>Name</th> |
138 |
<th>Email</th> |
139 |
<th>Password</th> |
140 |
</tr> |
141 |
</thead> |
142 |
<tbody> |
143 |
<tr> |
144 |
<td>John Doe</td> |
145 |
<td>john.doe@example.com</td> |
146 |
<td>johndoe1</td> |
147 |
</tr> |
148 |
</tbody> |
149 |
</table> |
150 |
</div> |
151 |
<button id="create-user">Create new user</button> |
152 |
|
153 |
<div class="demo-description"> |
154 |
<p>Use a modal dialog to require that the user enter data during a multi-step process. Embed form markup in the content area, set the <code>modal</code> option to true, and specify primary and secondary user actions with the <code>buttons</code> option.</p> |
155 |
</div> |
156 |
</body> |
157 |
</html> |