Dialog demo: Update modal form demo

- Removes an invalid jquery.ui.button.js reference (button.js is loaded)
- Updates the email regex to use the one from the HTML5 spec
- Refactors the code to add the user on both button click and form submit
- Reset the form to its original state on submit
- Initialize the form with values that can be submitted immediately, better
for a demo
- Rename bValid to valid
This commit is contained in:
Jörn Zaefferer 2014-04-24 19:19:50 +02:00
parent 28093e6896
commit b41b92213a

View File

@ -12,7 +12,6 @@
<script src="../../ui/draggable.js"></script> <script src="../../ui/draggable.js"></script>
<script src="../../ui/position.js"></script> <script src="../../ui/position.js"></script>
<script src="../../ui/resizable.js"></script> <script src="../../ui/resizable.js"></script>
<script src="../../ui/jquery.ui.button.js"></script>
<script src="../../ui/dialog.js"></script> <script src="../../ui/dialog.js"></script>
<script src="../../ui/effect.js"></script> <script src="../../ui/effect.js"></script>
<link rel="stylesheet" href="../demos.css"> <link rel="stylesheet" href="../demos.css">
@ -30,7 +29,11 @@
</style> </style>
<script> <script>
$(function() { $(function() {
var name = $( "#name" ), var dialog, form,
// From http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#e-mail-state-%28type=email%29
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])?)*$/,
name = $( "#name" ),
email = $( "#email" ), email = $( "#email" ),
password = $( "#password" ), password = $( "#password" ),
allFields = $( [] ).add( name ).add( email ).add( password ), allFields = $( [] ).add( name ).add( email ).add( password ),
@ -66,48 +69,54 @@
} }
} }
$( "#dialog-form" ).dialog({ function addUser() {
var valid = true;
allFields.removeClass( "ui-state-error" );
valid = valid && checkLength( name, "username", 3, 16 );
valid = valid && checkLength( email, "email", 6, 80 );
valid = valid && checkLength( password, "password", 5, 16 );
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." );
valid = valid && checkRegexp( email, emailRegex, "eg. ui@jquery.com" );
valid = valid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
if ( valid ) {
$( "#users tbody" ).append( "<tr>" +
"<td>" + name.val() + "</td>" +
"<td>" + email.val() + "</td>" +
"<td>" + password.val() + "</td>" +
"</tr>" );
dialog.dialog( "close" );
}
return valid;
}
dialog = $( "#dialog-form" ).dialog({
autoOpen: false, autoOpen: false,
height: 300, height: 300,
width: 350, width: 350,
modal: true, modal: true,
buttons: { buttons: {
"Create an account": function() { "Create an account": addUser,
var bValid = true;
allFields.removeClass( "ui-state-error" );
bValid = bValid && checkLength( name, "username", 3, 16 );
bValid = bValid && checkLength( email, "email", 6, 80 );
bValid = bValid && checkLength( password, "password", 5, 16 );
bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
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" );
bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
if ( bValid ) {
$( "#users tbody" ).append( "<tr>" +
"<td>" + name.val() + "</td>" +
"<td>" + email.val() + "</td>" +
"<td>" + password.val() + "</td>" +
"</tr>" );
$( this ).dialog( "close" );
}
},
Cancel: function() { Cancel: function() {
$( this ).dialog( "close" ); dialog.dialog( "close" );
} }
}, },
close: function() { close: function() {
allFields.val( "" ).removeClass( "ui-state-error" ); form[ 0 ].reset();
allFields.removeClass( "ui-state-error" );
} }
}); });
$( "#create-user" ) form = dialog.find( "form" ).on( "submit", function( event ) {
.button() event.preventDefault();
.click(function() { addUser();
$( "#dialog-form" ).dialog( "open" ); });
});
$( "#create-user" ).button().on( "click", function() {
dialog.dialog( "open" );
});
}); });
</script> </script>
</head> </head>
@ -117,14 +126,17 @@
<p class="validateTips">All form fields are required.</p> <p class="validateTips">All form fields are required.</p>
<form> <form>
<fieldset> <fieldset>
<label for="name">Name</label> <label for="name">Name</label>
<input type="text" name="name" id="name" class="text ui-widget-content ui-corner-all" /> <input type="text" name="name" id="name" value="Jane Smith" class="text ui-widget-content ui-corner-all">
<label for="email">Email</label> <label for="email">Email</label>
<input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all" /> <input type="text" name="email" id="email" value="jane@smith.com" class="text ui-widget-content ui-corner-all">
<label for="password">Password</label> <label for="password">Password</label>
<input type="password" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" /> <input type="password" name="password" id="password" value="xxxxxxx" class="text ui-widget-content ui-corner-all">
</fieldset>
<!-- Allow form submission with keyboard without duplicating the dialog button -->
<input type="submit" tabindex="-1" style="position:absolute; top:-1000px">
</fieldset>
</form> </form>
</div> </div>