| 1 |
<!doctype html> |
| 2 |
<html lang="en"> |
| 3 |
<head> |
| 4 |
<meta charset="utf-8"> |
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1"> |
| 6 |
<title>jQuery UI Dialog - Modal form</title> |
| 7 |
<link rel="stylesheet" href="../../themes/base/all.css"> |
| 8 |
<link rel="stylesheet" href="../demos.css"> |
| 9 |
<style> |
| 10 |
label, input { display:block; } |
| 11 |
input.text { margin-bottom:12px; width:95%; padding: .4em; } |
| 12 |
fieldset { padding:0; border:0; margin-top:25px; } |
| 13 |
h1 { font-size: 1.2em; margin: .6em 0; } |
| 14 |
div#users-contain { width: 350px; margin: 20px 0; } |
| 15 |
div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; } |
| 16 |
div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; } |
| 17 |
.ui-dialog .ui-state-error { padding: .3em; } |
| 18 |
.validateTips { border: 1px solid transparent; padding: 0.3em; } |
| 19 |
</style> |
| 20 |
<script src="../../external/requirejs/require.js"></script> |
| 21 |
<script src="../bootstrap.js" data-modules="effect"> |
| 22 |
var dialog, form, |
| 23 |
|
| 24 |
// From http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#e-mail-state-%28type=email%29 |
| 25 |
emailRegex = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/, |
| 26 |
name = $( "#name" ), |
| 27 |
email = $( "#email" ), |
| 28 |
password = $( "#password" ), |
| 29 |
allFields = $( [] ).add( name ).add( email ).add( password ), |
| 30 |
tips = $( ".validateTips" ); |
| 31 |
|
| 32 |
function updateTips( t ) { |
| 33 |
tips |
| 34 |
.text( t ) |
| 35 |
.addClass( "ui-state-highlight" ); |
| 36 |
setTimeout(function() { |
| 37 |
tips.removeClass( "ui-state-highlight", 1500 ); |
| 38 |
}, 500 ); |
| 39 |
} |
| 40 |
|
| 41 |
function checkLength( o, n, min, max ) { |
| 42 |
if ( o.val().length > max || o.val().length < min ) { |
| 43 |
o.addClass( "ui-state-error" ); |
| 44 |
updateTips( "Length of " + n + " must be between " + |
| 45 |
min + " and " + max + "." ); |
| 46 |
return false; |
| 47 |
} else { |
| 48 |
return true; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
function checkRegexp( o, regexp, n ) { |
| 53 |
if ( !( regexp.test( o.val() ) ) ) { |
| 54 |
o.addClass( "ui-state-error" ); |
| 55 |
updateTips( n ); |
| 56 |
return false; |
| 57 |
} else { |
| 58 |
return true; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
function addUser() { |
| 63 |
var valid = true; |
| 64 |
allFields.removeClass( "ui-state-error" ); |
| 65 |
|
| 66 |
valid = valid && checkLength( name, "username", 3, 16 ); |
| 67 |
valid = valid && checkLength( email, "email", 6, 80 ); |
| 68 |
valid = valid && checkLength( password, "password", 5, 16 ); |
| 69 |
|
| 70 |
valid = valid && checkRegexp( name, /^[a-z]([0-9a-z_\s])+$/i, "Username may consist of a-z, 0-9, underscores, spaces and must begin with a letter." ); |
| 71 |
valid = valid && checkRegexp( email, emailRegex, "eg. [email protected]" ); |
| 72 |
valid = valid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" ); |
| 73 |
|
| 74 |
if ( valid ) { |
| 75 |
$( "#users tbody" ).append( "<tr>" + |
| 76 |
"<td>" + name.val() + "</td>" + |
| 77 |
"<td>" + email.val() + "</td>" + |
| 78 |
"<td>" + password.val() + "</td>" + |
| 79 |
"</tr>" ); |
| 80 |
dialog.dialog( "close" ); |
| 81 |
} |
| 82 |
return valid; |
| 83 |
} |
| 84 |
|
| 85 |
dialog = $( "#dialog-form" ).dialog({ |
| 86 |
autoOpen: false, |
| 87 |
height: 400, |
| 88 |
width: 350, |
| 89 |
modal: true, |
| 90 |
buttons: { |
| 91 |
"Create an account": addUser, |
| 92 |
Cancel: function() { |
| 93 |
dialog.dialog( "close" ); |
| 94 |
} |
| 95 |
}, |
| 96 |
close: function() { |
| 97 |
form[ 0 ].reset(); |
| 98 |
allFields.removeClass( "ui-state-error" ); |
| 99 |
} |
| 100 |
}); |
| 101 |
|
| 102 |
form = dialog.find( "form" ).on( "submit", function( event ) { |
| 103 |
event.preventDefault(); |
| 104 |
addUser(); |
| 105 |
}); |
| 106 |
|
| 107 |
$( "#create-user" ).button().on( "click", function() { |
| 108 |
dialog.dialog( "open" ); |
| 109 |
}); |
| 110 |
</script> |
| 111 |
</head> |
| 112 |
<body> |
| 113 |
|
| 114 |
<div id="dialog-form" title="Create new user"> |
| 115 |
<p class="validateTips">All form fields are required.</p> |
| 116 |
|
| 117 |
<form> |
| 118 |
<fieldset> |
| 119 |
<label for="name">Name</label> |
| 120 |
<input type="text" name="name" id="name" value="Jane Smith" class="text ui-widget-content ui-corner-all"> |
| 121 |
<label for="email">Email</label> |
| 122 |
<input type="text" name="email" id="email" value="[email protected]" class="text ui-widget-content ui-corner-all"> |
| 123 |
<label for="password">Password</label> |
| 124 |
<input type="password" name="password" id="password" value="xxxxxxx" class="text ui-widget-content ui-corner-all"> |
| 125 |
|
| 126 |
<!-- Allow form submission with keyboard without duplicating the dialog button --> |
| 127 |
<input type="submit" tabindex="-1" style="position:absolute; top:-1000px"> |
| 128 |
</fieldset> |
| 129 |
</form> |
| 130 |
</div> |
| 131 |
|
| 132 |
|
| 133 |
<div id="users-contain" class="ui-widget"> |
| 134 |
<h1>Existing Users:</h1> |
| 135 |
<table id="users" class="ui-widget ui-widget-content"> |
| 136 |
<thead> |
| 137 |
<tr class="ui-widget-header "> |
| 138 |
<th>Name</th> |
| 139 |
<th>Email</th> |
| 140 |
<th>Password</th> |
| 141 |
</tr> |
| 142 |
</thead> |
| 143 |
<tbody> |
| 144 |
<tr> |
| 145 |
<td>John Doe</td> |
| 146 |
<td>[email protected]</td> |
| 147 |
<td>johndoe1</td> |
| 148 |
</tr> |
| 149 |
</tbody> |
| 150 |
</table> |
| 151 |
</div> |
| 152 |
<button id="create-user">Create new user</button> |
| 153 |
|
| 154 |
<div class="demo-description"> |
| 155 |
<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> |
| 156 |
</div> |
| 157 |
</body> |
| 158 |
</html> |