|
| 1 | +// Default.cs |
| 2 | +// Script#/samples/jQuery.UI/jQuery.UI.Sample/Button |
| 3 | +// Copyright (c) Ivaylo Gochkov, 2012 |
| 4 | +// This source code is subject to terms and conditions of the Apache License, Version 2.0. |
| 5 | +// |
| 6 | + |
| 7 | +using System; |
| 8 | +using System.Html; |
| 9 | +using jQueryApi; |
| 10 | +using jQueryApi.UI.Widgets; |
| 11 | + |
| 12 | +namespace Sample.Dialog { |
| 13 | + internal static class ModalForm { |
| 14 | + static ModalForm() { |
| 15 | + jQuery.OnDocumentReady(delegate() { |
| 16 | + jQueryObject name = jQuery.Select("#name"); |
| 17 | + jQueryObject email = jQuery.Select("#email"); |
| 18 | + jQueryObject password = jQuery.Select("#password"); |
| 19 | + jQueryObject allFields = jQuery.FromObject(new jQuery[] { }).Add(name).Add(email).Add(password); |
| 20 | + jQueryObject tips = jQuery.Select(".validateTips"); |
| 21 | + |
| 22 | + Action<string> updateTips = new Action<string>(delegate(string t) { |
| 23 | + tips.Text(t) |
| 24 | + .AddClass("ui-state-highlight"); |
| 25 | + |
| 26 | + Window.SetTimeout(new Action(delegate() { |
| 27 | + tips.RemoveClass("ui-state-highlight"); //, 1500 ); |
| 28 | + }), 500); |
| 29 | + }); |
| 30 | + |
| 31 | + Func<jQueryObject, string, int, int, bool> checkLength = new Func<jQueryObject, string, int, int, bool>( |
| 32 | + delegate(jQueryObject o, string n, int min, int max) { |
| 33 | + if (o.GetValue().Length > max || o.GetValue().Length < min) { |
| 34 | + o.AddClass("ui-state-error"); |
| 35 | + updateTips("Length of " + n + " must be between " + min + " and " + max + "."); |
| 36 | + return false; |
| 37 | + } else { |
| 38 | + return true; |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + Func<jQueryObject, RegularExpression, string, bool> checkRegexp = new Func<jQueryObject, RegularExpression, string, bool>( |
| 43 | + delegate(jQueryObject o, RegularExpression regexp, string n) { |
| 44 | + if (!(regexp.Test(o.GetValue()))) { |
| 45 | + o.AddClass("ui-state-error"); |
| 46 | + updateTips(n); |
| 47 | + return false; |
| 48 | + } else { |
| 49 | + return true; |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + jQuery.Select("#dialog-form") |
| 54 | + .Plugin<DialogObject>() |
| 55 | + .Dialog(new DialogOptions( |
| 56 | + DialogOption.AutoOpen, false, |
| 57 | + DialogOption.Height, 300, |
| 58 | + DialogOption.Width, 350, |
| 59 | + DialogOption.Modal, true, |
| 60 | + DialogOption.Buttons, new DialogOptions( |
| 61 | + "Create an account", new Action(delegate() { |
| 62 | + bool bValid = true; |
| 63 | + allFields.RemoveClass("ui-state-error"); |
| 64 | + |
| 65 | + bValid = bValid && checkLength(name, "username", 3, 16); |
| 66 | + bValid = bValid && checkLength(email, "email", 6, 80); |
| 67 | + bValid = bValid && checkLength(password, "password", 5, 16); |
| 68 | + |
| 69 | + bValid = bValid && checkRegexp(name, new RegularExpression("^[a-z]([0-9a-z_])+$"), "Username may consist of a-z, 0-9, underscores, begin with a letter."); |
| 70 | + // From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/ |
| 71 | + bValid = bValid && checkRegexp(email, new RegularExpression(@"^((([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])))\.?$"), "eg. [email protected]"); |
| 72 | + bValid = bValid && checkRegexp(password, new RegularExpression("^([0-9a-zA-Z])+$"), "Password field only allow : a-z 0-9"); |
| 73 | + |
| 74 | + if (bValid) { |
| 75 | + jQuery.Select("#users tbody") |
| 76 | + .Append("<tr>" + |
| 77 | + "<td>" + name.GetValue() + "</td>" + |
| 78 | + "<td>" + email.GetValue() + "</td>" + |
| 79 | + "<td>" + password.GetValue() + "</td>" + |
| 80 | + "</tr>"); |
| 81 | + jQuery.This.Plugin<DialogObject>() |
| 82 | + .Dialog(DialogMethod.Close); |
| 83 | + } |
| 84 | + }), |
| 85 | + "Cancel", new Action(delegate() { |
| 86 | + jQuery.This.Plugin<DialogObject>() |
| 87 | + .Dialog(DialogMethod.Close); |
| 88 | + })), |
| 89 | + DialogEvents.Close, new Action(delegate() { |
| 90 | + allFields.Value("").RemoveClass("ui-state-error"); |
| 91 | + }))); |
| 92 | + |
| 93 | + jQuery.Select("#create-user") |
| 94 | + .Plugin<ButtonObject>() |
| 95 | + .Button() |
| 96 | + .Click(new jQueryEventHandler(delegate(jQueryEvent e) { |
| 97 | + jQuery.Select("#dialog-form").Plugin<DialogObject>().Dialog(DialogMethod.Open); |
| 98 | + })); |
| 99 | + }); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments