Subversion-Projekte lars-tiefland.marine-sales.de

Revision

Blame | Letzte Änderung | Log anzeigen | RSS feed

<!DOCTYPE html>
<html>
<head>
        <meta charset="utf-8">
        <title>jQuery validation plug-in - ThemeRolldered demo</title>
        <link rel="stylesheet" media="screen" href="https://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.min.css">
        <style>
        body {
                font-size: 62.5%;
        }
        label {
                display: inline-block;
                width: 100px;
        }
        legend {
                padding: 0.5em;
        }
        fieldset fieldset label {
                display: block;
        }
        #commentForm {
                width: 500px;
        }
        #commentForm label {
                width: 250px;
        }
        #commentForm label.error, #commentForm button.submit {
                margin-left: 253px;
        }
        #signupForm {
                width: 670px;
        }
        #signupForm label.error {
                margin-left: 10px;
                width: auto;
                display: inline;
        }
        #newsletter_topics label.error {
                display: none;
                margin-left: 103px;
        }
        </style>
</head>
<body>
<form class="cmxform" id="commentForm" method="get" action="">
        <fieldset class="ui-widget ui-widget-content ui-corner-all">
                <legend class="ui-widget ui-widget-header ui-corner-all">Please provide your name, email address (won't be published) and a comment</legend>
                <p>
                        <label for="cname">Name (required, at least 2 characters)</label>
                        <input id="cname" name="name" class="ui-widget-content" minlength="2" required type="text">
                        <p>
                                <label for="cemail">E-Mail (required)</label>
                                <input id="cemail" name="email" class="ui-widget-content" type="email" required>
                        </p>
                        <p>
                                <label for="curl">URL (optional)</label>
                                <input id="curl" name="url" class="ui-widget-content" value="" type="url">
                        </p>
                        <p>
                                <label for="ccomment">Your comment (required)</label>
                                <textarea id="ccomment" name="comment" class="ui-widget-content" required></textarea>
                        </p>
                        <p>
                                <button class="submit" type="submit">Submit</button>
                        </p>
        </fieldset>
</form>
<form class="cmxform" id="signupForm" method="get" action="">
        <fieldset class="ui-widget ui-widget-content ui-corner-all">
                <legend class="ui-widget ui-widget-header ui-corner-all">Validating a complete form</legend>
                <p>
                        <label for="firstname">Firstname</label>
                        <input id="firstname" name="firstname" type="text">
                </p>
                <p>
                        <label for="lastname">Lastname</label>
                        <input id="lastname" name="lastname" type="text">
                </p>
                <p>
                        <label for="username">Username</label>
                        <input id="username" name="username" type="text">
                </p>
                <p>
                        <label for="password">Password</label>
                        <input id="password" name="password" type="password">
                </p>
                <p>
                        <label for="confirm_password">Confirm password</label>
                        <input id="confirm_password" name="confirm_password" type="password">
                </p>
                <p>
                        <label for="email">Email</label>
                        <input id="email" name="email" type="email">
                </p>
                <p>
                        <label for="agree">Please agree to our policy</label>
                        <input type="checkbox" class="checkbox" id="agree" name="agree">
                </p>
                <p>
                        <label for="newsletter">I'd like to receive the newsletter</label>
                        <input type="checkbox" class="checkbox" id="newsletter" name="newsletter">
                </p>
                <fieldset id="newsletter_topics" class="ui-widget-content ui-corner-all">
                        <legend class="ui-widget-header ui-corner-all">Topics (select at least two) - note: would be hidden when newsletter isn't selected, but is visible here for the demo</legend>
                        <label for="topic_marketflash">
                                <input type="checkbox" id="topic_marketflash" value="marketflash" name="topic">Marketflash
                        </label>
                        <label for="topic_fuzz">
                                <input type="checkbox" id="topic_fuzz" value="fuzz" name="topic">Latest fuzz
                        </label>
                        <label for="topic_digester">
                                <input type="checkbox" id="topic_digester" value="digester" name="topic">Mailing list digester
                        </label>
                        <label for="topic" class="error">Please select at least two topics you'd like to receive.</label>
                </fieldset>
                <p>
                        <button class="submit" type="submit">Submit</button>
                </p>
        </fieldset>
</form>
<script src="../lib/jquery.js"></script>
<script src="../dist/jquery.validate.js"></script>
<script src="https://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>
<script>
$.validator.setDefaults({
        submitHandler: function() {
                alert("submitted!");
        },
        showErrors: function(map, list) {
                // there's probably a way to simplify this
                var focussed = document.activeElement;
                if (focussed && $(focussed).is("input, textarea")) {
                        $(this.currentForm).tooltip("close", {
                                currentTarget: focussed
                        }, true)
                }
                this.currentElements.removeAttr("title").removeClass("ui-state-highlight");
                $.each(list, function(index, error) {
                        $(error.element).attr("title", error.message).addClass("ui-state-highlight");
                });
                if (focussed && $(focussed).is("input, textarea")) {
                        $(this.currentForm).tooltip("open", {
                                target: focussed
                        });
                }
        }
});

(function() {
        // use custom tooltip; disable animations for now to work around lack of refresh method on tooltip
        $("#commentForm, #signupForm").tooltip({
                show: false,
                hide: false
        });

        // validate the comment form when it is submitted
        $("#commentForm").validate();

        // validate signup form on keyup and submit
        $("#signupForm").validate({
                rules: {
                        firstname: "required",
                        lastname: "required",
                        username: {
                                required: true,
                                minlength: 2
                        },
                        password: {
                                required: true,
                                minlength: 5
                        },
                        confirm_password: {
                                required: true,
                                minlength: 5,
                                equalTo: "#password"
                        },
                        email: {
                                required: true,
                                email: true
                        },
                        topic: {
                                required: "#newsletter:checked",
                                minlength: 2
                        },
                        agree: "required"
                },
                messages: {
                        firstname: "Please enter your firstname",
                        lastname: "Please enter your lastname",
                        username: {
                                required: "Please enter a username",
                                minlength: "Your username must consist of at least 2 characters"
                        },
                        password: {
                                required: "Please provide a password",
                                minlength: "Your password must be at least 5 characters long"
                        },
                        confirm_password: {
                                required: "Please provide a password",
                                minlength: "Your password must be at least 5 characters long",
                                equalTo: "Please enter the same password as above"
                        },
                        email: "Please enter a valid email address",
                        agree: "Please accept our policy"
                }
        });

        // propose username by combining first- and lastname
        $("#username").focus(function() {
                var firstname = $("#firstname").val();
                var lastname = $("#lastname").val();
                if (firstname && lastname && !this.value) {
                        this.value = firstname + "." + lastname;
                }
        });

        //code to hide topic selection, disable for demo
        var newsletter = $("#newsletter");
        // newsletter topics are optional, hide at first
        var inital = newsletter.is(":checked");
        var topics = $("#newsletter_topics")[inital ? "removeClass" : "addClass"]("gray");
        var topicInputs = topics.find("input").attr("disabled", !inital);
        // show when newsletter is checked
        newsletter.click(function() {
                topics[this.checked ? "removeClass" : "addClass"]("gray");
                topicInputs.attr("disabled", !this.checked);
        });

        $("#signupForm input:not(:submit)").addClass("ui-widget-content");

        $(":submit").button();
})();
</script>
</body>
</html>