本篇使用Dialog 构造一个比较实用的对话框,它可以内嵌一个表单用来接受用户输入,每个输入框支持数据校验,部分使用正则表达式来检验。
5 |
< title >jQuery UI Demos</ title > |
6 |
< link rel = "stylesheet" href = "themes/trontastic/jquery-ui.css" /> |
7 |
< script src = "scripts/jquery-1.9.1.js" ></ script > |
8 |
< script src = "scripts/jquery-ui-1.10.1.custom.js" ></ script > |
40 |
div#users-contain table { |
42 |
border-collapse: collapse; |
46 |
div#users-contain table td, div#users-contain table th { |
47 |
border: 1px solid #eee; |
52 |
.ui-dialog .ui-state-error { |
57 |
border: 1px solid transparent; |
64 |
var name = $("#name"), |
66 |
password = $("#password"), |
67 |
allFields = $([]).add(name).add(email).add(password), |
68 |
tips = $(".validateTips"); |
70 |
function updateTips(t) { |
73 |
.addClass("ui-state-highlight"); |
74 |
setTimeout(function () { |
75 |
tips.removeClass("ui-state-highlight", 1500); |
79 |
function checkLength(o, n, min, max) { |
80 |
if (o.val().length > max || o.val().length < min ) { |
81 |
o.addClass("ui-state-error"); |
82 |
updateTips("Length of " + n + " must be between " + |
83 |
min + " and " + max + "."); |
90 |
function checkRegexp(o, regexp, n) { |
91 |
if (!(regexp.test(o.val()))) { |
92 |
o.addClass("ui-state-error"); |
100 |
$("#dialog-form").dialog({ |
106 |
"Create an account": function () { |
108 |
allFields.removeClass("ui-state-error"); |
110 |
bValid = bValid && checkLength(name, "username", 3, 16); |
111 |
bValid = bValid && checkLength(email, "email", 6, 80); |
112 |
bValid = bValid && checkLength(password, "password", 5, 16); |
114 |
bValid = bValid && checkRegexp(name, /^[a-z]([0-9a-z_])+$/i, |
115 |
"Username may consist of a-z, 0-9, " + |
116 |
"underscores, begin with a letter."); |
117 |
// From jquery.validate.js (by joern), |
118 |
// contributed by Scott Gonzalez: |
119 |
// http://projects.scottsplayground.com/email_address_validation/ |
120 |
bValid = bValid && checkRegexp(email, |
121 |
/^((([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"); |
122 |
bValid = bValid && checkRegexp(password, |
124 |
"Password field only allow : a-z 0-9"); |
127 |
$("#users tbody").append("<tr>" + |
128 |
"< td >" + name.val() + "</ td >" + |
129 |
"< td >" + email.val() + "</ td >" + |
130 |
"< td >" + password.val() + "</ td >" + |
132 |
$(this).dialog("close"); |
135 |
Cancel: function () { |
136 |
$(this).dialog("close"); |
140 |
allFields.val("").removeClass("ui-state-error"); |
147 |
$("#dialog-form").dialog("open"); |
155 |
< div id = "dialog-form" title = "Create new user" > |
156 |
< p class = "validateTips" >All form fields are required.</ p > |
160 |
< label for = "name" >Name</ label > |
161 |
< input type = "text" name = "name" id = "name" |
162 |
class = "text ui-widget-content ui-corner-all" /> |
163 |
< label for = "email" >Email</ label > |
164 |
< input type = "text" name = "email" id = "email" |
165 |
value = "" class = "text ui-widget-content ui-corner-all" /> |
166 |
< label for = "password" >Password</ label > |
167 |
< input type = "password" name = "password" |
168 |
id = "password" value = "" |
169 |
class = "text ui-widget-content ui-corner-all" /> |
175 |
< div id = "users-contain" class = "ui-widget" > |
176 |
< h1 >Existing Users:</ h1 > |
177 |
< table id = "users" class = "ui-widget ui-widget-content" > |
179 |
< tr class = "ui-widget-header " > |
188 |
< td >john.doe@example.com</ td > |
194 |
< button id = "create-user" >Create new user</ button > |
