夜光带你走进Jquery(二十三)擅长的领域

夜光序言:

 

时代的意志是如此强大, 每个逆流者都代价惨重。

 

 

 

 

 

 

 

 

 

正文:

jQuery Validate

jQuery Validate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求

默认校验规则

序号规则描述
1required:true必须输入的字段。
2remote:"check.php"使用 ajax 方法调用 check.php 验证输入值。
3email:true必须输入正确格式的电子邮件。
4url:true必须输入正确格式的网址。
5date:true必须输入正确格式的日期。日期校验 ie6 出错,慎用。
6dateISO:true必须输入正确格式的日期(ISO),例如:2009-06-23,1998/01/22。只验证格式,不验证有效性。
7number:true必须输入合法的数字(负数,小数)。
8digits:true必须输入整数。
9creditcard:必须输入合法的信用卡号。
10equalTo:"#field"输入值必须和 #field 相同。
11accept:输入拥有合法后缀名的字符串(上传文件的后缀)。
12maxlength:5输入长度最多是 5 的字符串(汉字算一个字符)。
13minlength:10输入长度最小是 10 的字符串(汉字算一个字符)。
14rangelength:[5,10]输入长度必须介于 5 和 10 之间的字符串(汉字算一个字符)。
15range:[5,10]输入值必须介于 5 和 10 之间。
16max:5输入值不能大于 5。
17min:10输入值不能小于 10。

 

引入 jQuery Validate

官网下载地址:https://github.com/jquery-validation/jquery-validation/releases

根据项目需要引入需要的 Js 库文件到 <head> 标签下或者在 <body> 标签关闭之前(推荐)。

 

 

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>测试 jQuery validate() 插件</title>
    <link rel="stylesheet" media="screen" href="screen.css">
    <script src="jquery.js"></script>
    <script src="jquery.validate.min.js"></script>
    <script src="messages_zh.js"></script>
    <style>
        .cmxform fieldset p.error label {
            color: red;
        }
        div.container {
            background-color: #eee;
            border: 1px solid red;
            margin: 5px;
            padding: 5px;
        }
        div.container ol li {
            list-style-type: disc;
            margin-left: 20px;
        }
        div.container {
            display: none
        }
        .container label.error {
            display: inline;
        }
        form.cmxform {
            width: 30em;
        }
        form.cmxform label.error {
            display: block;
            margin-left: 1em;
            width: auto;
        }
    </style>
    <script>
        // 只用于测试
        $.validator.setDefaults({
            submitHandler: function() {
                alert("提交事件! (取消跳过验证)");
            }
        });

        $().ready(function() {
            $("#form1").validate({
                errorLabelContainer: $("#form1 div.error")
            });

            var container = $('div.container');
            // 提交时验证表单
            var validator = $("#form2").validate({
                errorContainer: container,
                errorLabelContainer: $("ol", container),
                wrapper: 'li'
            });

            $(".cancel").click(function() {
                validator.resetForm();
            });
        });
    </script>
</head>

<body>
<div id="main">
    <form method="get" class="cmxform" id="form1" action="">
        <fieldset>
            <legend>登录框</legend>
            <p>
                <label>用户名</label>
                <input name="user" title="请输入用户名 (至少三个字母)" required minlength="3">
            </p>
            <p>
                <label>密码</label>
                <input type="password" maxlength="12" name="password" title="请输入密码,字母在 5 到 12 个之间" required minlength="5">
            </p>
            <div class="error">
            </div>
            <p>
                <input class="submit" type="submit" value="Login">
            </p>
        </fieldset>
    </form>
    <!-- our error container -->
    <div class="container">
        <h4>在你提交表单时出现了以下错误,详情如下:</h4>
        <ol>
            <li>
                <label for="email" class="error">请输入邮箱地址。</label>
            </li>
            <li>
                <label for="phone" class="error">请输入电话号码 ( 2 到 8 个字母)</label>
            </li>
            <li>
                <label for="address" class="error">请输入地址 (至少三个字母)</label>
            </li>
            <li>
                <label for="avatar" class="error">请选择图片 (png, jpg, jpeg, gif)</label>
            </li>
            <li>
                <label for="cv" class="error">请选择文档 (doc, docx, txt, pdf)</label>
            </li>
        </ol>
    </div>
    <form class="cmxform" id="form2" method="get" action="">
        <fieldset>
            <legend>验证完整表单</legend>
            <p>
                <label for="email">Email</label>
                <input id="email" name="email" required type="email">
            </p>
            <p>
                <label for="agree">喜欢的颜色</label>
                <select id="color" name="color" title="请选择你喜欢的颜色!" required>
                    <option></option>
                    <option>Red</option>
                    <option>Blue</option>
                    <option>Yellow</option>
                </select>
            </p>
            <p>
                <label for="phone">电话</label>
                <input id="phone" name="phone" required type="number" rangelength="[2,8]">
            </p>
            <p>
                <label for="address">地址</label>
                <input id="address" name="address" required minlength="3">
            </p>
            <p>
                <label for="avatar">头像</label>
                <input type="file" id="avatar" name="avatar" required>
            </p>
            <p>
                <label for="agree">请同意我们的声明</label>
                <input type="checkbox" class="checkbox" id="agree" title="请同意我们的声明!" name="agree" required>
            </p>
            <p>
                <input class="submit" type="submit" value="提交">
                <input class="cancel" type="submit" value="取消">
            </p>
        </fieldset>
    </form>
</div>
</body>
</html>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>jQuery Validate 插件 - 自定义消息作为元素数据</title>

    <link rel="stylesheet" media="screen" href="screen.css">
    <script src="jquery.js"></script>
    <script src="jquery.validate.min.js"></script>
    <script src="messages_zh.js"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            $("#commentForm").validate();
            $("#commentForm2").validate({
                messages: {
                    email: {
                        required: '请输入!'
                    }
                }
            });

        });
    </script>

    <style type="text/css">
        form { width: 500px; }
        form label { width: 250px; }
        form label.error,
        form input.submit { margin-left: 253px; }
    </style>

</head>
<body>

<div id="main">

    <p>夜光:演示了如何通过元数据自定义消息。</p>

    <!-- 通过 data- 属性自定义规则和消息 -->
    <form class="cmxform" id="commentForm" method="post" action="">
        <fieldset>
            <legend>请输入您的电子邮件地址</legend>
            <p>

                <label for="cemail">E-Mail *</label>
                <input id="cemail" name="email" data-rule-required="true" data-rule-email="true" data-msg-required="请输入您的电子邮件地址" data-msg-email="请输入一个有效的电子邮件地址" />
            </p>
            <p>
                <input class="submit" type="submit" value="提交"/>
            </p>
        </fieldset>
    </form>

    <!-- 自定义通过 validate 选项重载的元数据中 "required" 的消息 -->
    <form class="cmxform" id="commentForm2" method="post" action="">
        <fieldset>
            <legend>请输入您的电子邮件地址</legend>
            <p>

                <label for="cemail">E-Mail *</label>
                <input id="cemail" name="email" data-rule-required="true" data-rule-email="true" data-msg-email="请输入一个有效的电子邮件地址" />
            </p>
            <p>
                <input class="submit" type="submit" value="提交"/>
            </p>
        </fieldset>
    </form>

</body>
</html>

 

 

 

 

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>很帅:jQuery Validate 插件 - radio(单选按钮)、checkbox(复选按钮)和 select(下拉框)</title>

    <link rel="stylesheet" media="screen" href="screen.css">
    <script src="jquery.js"></script>
    <script src="jquery.validate.min.js"></script>
    <script src="messages_zh.js"></script>

    <script>
        // 只用于演示
        $.validator.setDefaults({
            submitHandler: function() {
                alert("submitted!");
            }
        });

        $(document).ready(function() {
            $("#form1").validate();
            $("#selecttest").validate();
        });
    </script>

    <style>
        .block { display: block; }
        form.cmxform label.error { display: none; }
    </style>

</head>
<body>

<div id="main">

    <form class="cmxform" id="form1" method="get" action="">
        <fieldset>
            <legend>通过 radio(单选按钮)和 checkbox(复选按钮)验证表单</legend>
            <fieldset>
                <legend>性别</legend>
                <label for="gender_male">
                    <input  type="radio" id="gender_male" value="m" name="gender" required>
                    男性
                </label>
                <label for="gender_female">
                    <input  type="radio" id="gender_female" value="f" name="gender">
                    女性
                </label>
                <label for="gender" class="error">请选择您的性别。</label>
            </fieldset>
            <fieldset>
                <legend>婚姻状况</legend>
                <label for="family_single">
                    <input  type="radio" id="family_single" value="s" name="family" required>
                    单身
                </label>
                <label for="family_married">
                    <input  type="radio" id="family_married" value="m" name="family">
                    已婚
                </label>
                <label for="family_other">
                    <input  type="radio" id="family_other" value="o" name="family">
                    其他
                </label>
                <label for="family" class="error">您选择您的婚姻状况。</label>
            </fieldset>
            <p>
                <label for="agree">请同意我们的条款</label>
                <input type="checkbox" class="checkbox" id="agree" name="agree" required>
                <br>
                <label for="agree" class="error block">请同意我们的条款!</label>
            </p>
            <fieldset>
                <legend>垃圾邮件</legend>
                <label for="spam_email">
                    <input type="checkbox" class="checkbox" id="spam_email" value="email" name="spam[]" required minlength="2">
                    垃圾邮件 - E-Mail
                </label>
                <label for="spam_phone">
                    <input type="checkbox" class="checkbox" id="spam_phone" value="phone" name="spam[]">
                    垃圾邮件 - Phone
                </label>
                <label for="spam_mail">
                    <input type="checkbox" class="checkbox" id="spam_mail" value="mail" name="spam[]">
                    垃圾邮件 - Mail
                </label>
                <label for="spam[]" class="error">请选择至少两种类型的垃圾邮件。</label>
            </fieldset>
            <p>
                <input class="submit" type="submit" value="提交">
            </p>
        </fieldset>
    </form>

    <form id="selecttest">
        <h2>一些关于 select 的测试</h2>
        <p>
            <label for="jungle">请选择一个丛林名词</label><br>
            <select id="jungle" name="jungle" title="请选择一个丛林名词!" required>
                <option value=""></option>
                <option value="1">Buga</option>
                <option value="2">Baga</option>
                <option value="3">Oi</option>
            </select>
        </p>

        <p>
            <label for="fruit">请选择至少两种水果</label><br>
            <select id="fruit" name="fruit" title="请选择至少两种水果!" required minlength="2" multiple="multiple">
                <option value="b">Banana</option>
                <option value="a">Apple</option>
                <option value="p">Peach</option>
                <option value="t">Turtle</option>
            </select>
        </p>

        <p>
            <label for="vegetables">请选择不超过两种蔬菜</label><br>
            <select id="vegetables" name="vegetables" title="请选择不超过两种蔬菜!" required maxlength="2" multiple="multiple">
                <option value="p">Potato</option>
                <option value="t">Tomato</option>
                <option value="s">Salad</option>
            </select>
        </p>

        <p>
            <label for="cars">请选择至少两种但不超过三种汽车</label><br>
            <select id="cars" name="cars" title="请选择至少两种但不超过三种汽车!" required rangelength="[2,3]" multiple="multiple">
                <option value="m_sl">Mercedes SL</option>
                <option value="o_c">Opel Corsa</option>
                <option value="vw_p">VW Polo</option>
                <option value="t_s">Titanic Skoda</option>
            </select>
        </p>

        <p><input type="submit" value="Validate Select 测试"></p>
    </form>

</div>


</body>
</html>

 

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>夜光:jQuery validation 插件 - ThemeRolldered 实例</title>
    <link rel="stylesheet" media="screen" href="//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">请提供您的名字、电子邮件地址(不会被公布)和评论</legend>
        <p>
            <label for="cname">请提供您的名字、电子邮件地址(不会被公布)和评论</label>
            <input id="cname" name="name" class="ui-widget-content" minlength="2" required type="text">
        <p>
            <label for="cemail">E-Mail(必填)</label>
            <input id="cemail" name="email" class="ui-widget-content" type="email" required>
        </p>
        <p>
            <label for="curl">E-Mail(必填)</label>
            <input id="curl" name="url" class="ui-widget-content" value="" type="url">
        </p>
        <p>
            <label for="ccomment">您的评论(必填)</label>
            <textarea id="ccomment" name="comment" class="ui-widget-content" required></textarea>
        </p>
        <p>
            <button class="submit" type="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">验证一个完整的表单</legend>
        <p>
            <label for="firstname">名字</label>
            <input id="firstname" name="firstname" type="text">
        </p>
        <p>
            <label for="lastname">姓氏</label>
            <input id="lastname" name="lastname" type="text">
        </p>
        <p>
            <label for="username">用户名</label>
            <input id="username" name="username" type="text">
        </p>
        <p>
            <label for="password">密码</label>
            <input id="password" name="password" type="password">
        </p>
        <p>
            <label for="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">请同意我们的条款</label>
            <input type="checkbox" class="checkbox" id="agree" name="agree">
        </p>
        <p>
            <label for="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">主题(至少选择两个)- 注:当未选择简讯时隐藏该项,但此处为了演示所以可见</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">请选择至少两个您感兴趣的主题。</label>
        </fieldset>
        <p>
            <button class="submit" type="submit">提交</button>
        </p>
    </fieldset>
</form>
<script src="jquery.js"></script>
<script src="jquery.validate.min.js"></script>
<script src="messages_zh.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: "请输入您的名字",
                lastname: "请输入您的姓氏",
                username: {
                    required: "请输入一个用户名",
                    minlength: "您的用户名必须包含至少 2 个字符"
                },
                password: {
                    required: "请提供一个密码",
                    minlength: "您的密码必须包含至少 5 个字符"
                },
                confirm_password: {
                    required: "请提供一个密码",
                    minlength: "您的密码必须包含至少 5 个字符",
                    equalTo: "请输入与上面相同的密码"
                },
                email: "请输入一个有效的电子邮件地址",
                agree: "请接受我们的条款"
            }
        });

        // 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>

 

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>Remember The Milk signup form - jQuery Validate plugin demo - with friendly permission from the RTM team</title>
	<link rel="stylesheet" href="milk.css">
	<script src="../../lib/jquery.js"></script>
	<script src="../../lib/jquery.mockjax.js"></script>
	<script src="../../dist/jquery.validate.js"></script>
	<script>
	$(document).ready(function() {
		$.mockjax({
			url: "emails.action",
			response: function(settings) {
				var email = settings.data.email,
					emails = ["glen@marketo.com", "george@bush.gov", "me@god.com", "aboutface@cooper.com", "steam@valve.com", "bill@gates.com"];
				this.responseText = "true";
				if ($.inArray(email, emails) !== -1) {
					this.responseText = "false";
				}
			},
			responseTime: 500
		});

		$.mockjax({
			url: "users.action",
			response: function(settings) {
				var user = settings.data.username,
					users = ["asdf", "Peter", "Peter2", "George"];
				this.responseText = "true";
				if ($.inArray(user, users) !== -1) {
					this.responseText = "false";
				}
			},
			responseTime: 500
		});

		// validate signup form on keyup and submit
		var validator = $("#signupform").validate({
			rules: {
				firstname: "required",
				lastname: "required",
				username: {
					required: true,
					minlength: 2,
					remote: "users.action"
				},
				password: {
					required: true,
					minlength: 5
				},
				password_confirm: {
					required: true,
					minlength: 5,
					equalTo: "#password"
				},
				email: {
					required: true,
					email: true,
					remote: "emails.action"
				},
				dateformat: "required",
				terms: "required"
			},
			messages: {
				firstname: "Enter your firstname",
				lastname: "Enter your lastname",
				username: {
					required: "Enter a username",
					minlength: jQuery.validator.format("Enter at least {0} characters"),
					remote: jQuery.validator.format("{0} is already in use")
				},
				password: {
					required: "Provide a password",
					minlength: jQuery.validator.format("Enter at least {0} characters")
				},
				password_confirm: {
					required: "Repeat your password",
					minlength: jQuery.validator.format("Enter at least {0} characters"),
					equalTo: "Enter the same password as above"
				},
				email: {
					required: "Please enter a valid email address",
					minlength: "Please enter a valid email address",
					remote: jQuery.validator.format("{0} is already in use")
				},
				dateformat: "Choose your preferred dateformat",
				terms: " "
			},
			// the errorPlacement has to take the table layout into account
			errorPlacement: function(error, element) {
				if (element.is(":radio"))
					error.appendTo(element.parent().next().next());
				else if (element.is(":checkbox"))
					error.appendTo(element.next());
				else
					error.appendTo(element.parent().next());
			},
			// specifying a submitHandler prevents the default submit, good for the demo
			submitHandler: function() {
				alert("submitted!");
			},
			// set this class to error-labels to indicate valid fields
			success: function(label) {
				// set &nbsp; as text for IE
				label.html("&nbsp;").addClass("checked");
			},
			highlight: function(element, errorClass) {
				$(element).parent().next().find("." + errorClass).removeClass("checked");
			}
		});

		// 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).toLowerCase();
			}
		});
	});
	</script>
</head>
<body>
<h1 id="banner"><a href="https://jqueryvalidation.org/">jQuery Validation Plugin</a> Demo</h1>
<div id="main">
	<div id="content">
		<div id="header">
			<div id="headerlogo">
				<img src="milk.png" alt="Remember The Milk">
			</div>
		</div>
		<div style="clear: both;">
			<div></div>
		</div>
		<div class="content">
			<div id="signupbox">
				<div id="signuptab">
					<ul>
						<li id="signupcurrent"><a href=" ">Signup</a>
						</li>
					</ul>
				</div>
				<div id="signupwrap">
					<form id="signupform" autocomplete="off" method="get" action="">
						<table>
							<tr>
								<td class="label">
									<label id="lfirstname" for="firstname">First Name</label>
								</td>
								<td class="field">
									<input id="firstname" name="firstname" type="text" value="" maxlength="100">
								</td>
								<td class="status"></td>
							</tr>
							<tr>
								<td class="label">
									<label id="llastname" for="lastname">Last Name</label>
								</td>
								<td class="field">
									<input id="lastname" name="lastname" type="text" value="" maxlength="100">
								</td>
								<td class="status"></td>
							</tr>
							<tr>
								<td class="label">
									<label id="lusername" for="username">Username</label>
								</td>
								<td class="field">
									<input id="username" name="username" type="text" value="" maxlength="50">
								</td>
								<td class="status"></td>
							</tr>
							<tr>
								<td class="label">
									<label id="lpassword" for="password">Password</label>
								</td>
								<td class="field">
									<input id="password" name="password" type="password" maxlength="50" value="">
								</td>
								<td class="status"></td>
							</tr>
							<tr>
								<td class="label">
									<label id="lpassword_confirm" for="password_confirm">Confirm Password</label>
								</td>
								<td class="field">
									<input id="password_confirm" name="password_confirm" type="password" maxlength="50" value="">
								</td>
								<td class="status"></td>
							</tr>
							<tr>
								<td class="label">
									<label id="lemail" for="email">Email Address</label>
								</td>
								<td class="field">
									<input id="email" name="email" type="text" value="" maxlength="150">
								</td>
								<td class="status"></td>
							</tr>
							<tr>
								<td class="label">
									<label>Which Looks Right</label>
								</td>
								<td class="field" colspan="2" style="vertical-align: top; padding-top: 2px;">
									<table>
										<tbody>
											<tr>
												<td style="padding-right: 5px;">
													<input id="dateformat_eu" name="dateformat" type="radio" value="0">
													<label id="ldateformat_eu" for="dateformat_eu">14/02/07</label>
												</td>
												<td style="padding-left: 5px;">
													<input id="dateformat_am" name="dateformat" type="radio" value="1">
													<label id="ldateformat_am" for="dateformat_am">02/14/07</label>
												</td>
												<td>
												</td>
											</tr>
										</tbody>
									</table>
								</td>
							</tr>
							<tr>
								<td class="label">&nbsp;</td>
								<td class="field" colspan="2">
									<div id="termswrap">
										<input id="terms" type="checkbox" name="terms">
										<label id="lterms" for="terms">I have read and accept the Terms of Use.</label>
									</div>
									<!-- /termswrap -->
								</td>
							</tr>
							<tr>
								<td class="label">
									<label id="lsignupsubmit" for="signupsubmit">Signup</label>
								</td>
								<td class="field" colspan="2">
									<input id="signupsubmit" name="signup" type="submit" value="Signup">
								</td>
							</tr>
						</table>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
</body>
</html>

 

 

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>jQuery accordion form with validation</title>
	<script src="../../lib/jquery.js"></script>
	<script src="../../dist/jquery.validate.js"></script>
	<script src="../marketo/jquery.maskedinput.js"></script>
	<script src="https://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>
	<script>
	$(document).ready(function() {
		$("#recordClientPhone").mask("(999) 999-9999");
		$("#recordClientPhoneAlt").mask("(999) 999-9999");
		$("#recordClientZip").mask("99999");
		$("#recordPropertyZip").mask("99999");
		$("#recordPurchaseZip").mask("99999");

		// add * to required field labels
		$('label.required').append('&nbsp;<strong>*</strong>&nbsp;');

		// accordion functions
		var accordion = $("#stepForm").accordion();
		var current = 0;

		$.validator.addMethod("pageRequired", function(value, element) {
			var $element = $(element)

				function match(index) {
					return current == index && $(element).parents("#sf" + (index + 1)).length;
				}
			if (match(0) || match(1) || match(2)) {
				return !this.optional(element);
			}
			return "dependency-mismatch";
		}, $.validator.messages.required)

		var v = $("#cmaForm").validate({
			errorClass: "warning",
			onkeyup: false,
			onfocusout: false,
			submitHandler: function() {
				alert("Submitted, thanks!");
			}
		});

		// back buttons do not need to run validation
		$("#sf2 .prevbutton").click(function() {
			accordion.accordion("option", "active", 0);
			current = 0;
		});
		$("#sf3 .prevbutton").click(function() {
			accordion.accordion("option", "active", 1);
			current = 1;
		});
		// these buttons all run the validation, overridden by specific targets above
		$(".open2").click(function() {
			if (v.form()) {
				accordion.accordion("option", "active", 2);
				current = 2;
			}
		});
		$(".open1").click(function() {
			if (v.form()) {
				accordion.accordion("option", "active", 1);
				current = 1;
			}
		});
		$(".open0").click(function() {
			if (v.form()) {
				accordion.accordion("option", "active", 0);
				current = 0;
			}
		});
	});
	</script>
	<link rel="stylesheet" media="screen" href="style.css">
</head>
<body>
<div id="wrap">
	<div id="main">
		<h1 class="top bottom">
			<span>Help me</span>Buy and Sell a House</h1>
		<h2>This form is quick &amp; easy to complete - in only 3 steps!</h2>
		<form name="cmaForm" id="cmaForm" method="post">
			<input type="hidden" name="recordRequestPrimaryServiceID" id="recordRequestPrimaryServiceID" value="100">
			<input type="hidden" name="recordClientServices" id="recordClientServices" value="1,3">
			<ul id="stepForm" class="ui-accordion-container">
				<li id="sf1">
					<a href='#' class="ui-accordion-link"></a>
					<div>
						<fieldset>
							<legend>Step 1 of 3</legend>
							<div class="requiredNotice">*Required Field</div>
							<h3 class="stepHeader">Tell us about the property you're buying</h3>
							<label for="recordPurchaseMetRealtor" class="input required">Are you currently working with a
								<br>real estate agent?</label>&nbsp;&nbsp;No:
							<input name="recordPurchaseMetRealtor" type="radio" checked="checked" class="inputclass" value="0">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Yes:
							<input name="recordPurchaseMetRealtor" type="radio" class="inputclass pageRequired" value="1" title="Please choose Yes or No">
							<div class="formspacer"></div>
							<label for="recordPurchaseTimeFrameID" class="input required">When would you like to move?</label>
							<select name="recordPurchaseTimeFrameID" id="recordPurchaseTimeFrameID" class="inputclass pageRequired" title="Select a Time Frame">
								<option value="">-Select-</option>
								<option value="1">Less than 3 months</option>
								<option value="2">3-6 months</option>
								<option value="3">6-9 months</option>
								<option value="4">9-12 months</option>
								<option value="5">Over 12 months</option>
							</select>
							<br>
							<label for="recordPurchasePriceRangeID" class="input required">Purchase price range:</label>
							<select name="recordPurchasePriceRangeID" id="recordPurchasePriceRangeID" class="inputclass pageRequired" title="Select a Price Range">
								<option value="">-Select-</option>
								<option value="1"></option>
								<option value="2">$75,000 - $100,000</option>
								<option value="3">$100,000 - $125,000</option>
								<option value="4">$125,000 - $150,000</option>
								<option value="5">$150,000 - $200,000</option>
								<option value="6">$200,000 - $250,000</option>
								<option value="7">$250,000 - $300,000</option>
								<option value="8">$300,000 - $350,000</option>
								<option value="9">$350,000 - $400,000</option>
								<option value="10">$400,000 - $500,000</option>
								<option value="11">$500,000 - $700,000</option>
								<option value="12">$700,000 - $900,000</option>
								<option value="13">> $900,000</option>
							</select>
							<br>
							<label for="recordPurchaseState" class="input required">State:</label>
							<select name="recordPurchaseState" id="recordPurchaseState" class="inputclass pageRequired" title="Select a State">
								<option value="">-Select-</option>
								<option value="AL">Alabama</option>
								<option value="AK">Alaska</option>
								<option value="AZ">Arizona</option>
								<option value="AR">Arkansas</option>
								<option value="CA">California</option>
								<option value="CO">Colorado</option>
								<option value="CT">Connecticut</option>
								<option value="DE">Delaware</option>
								<option value="DC">Dist of Columbia</option>
								<option value="FL">Florida</option>
								<option value="GA">Georgia</option>
								<option value="HI">Hawaii</option>
								<option value="ID">Idaho</option>
								<option value="IL">Illinois</option>
								<option value="IN">Indiana</option>
								<option value="IA">Iowa</option>
								<option value="KS">Kansas</option>
								<option value="KY">Kentucky</option>
								<option value="LA">Louisiana</option>
								<option value="ME">Maine</option>
								<option value="MD">Maryland</option>
								<option value="MA">Massachusetts</option>
								<option value="MI">Michigan</option>
								<option value="MN">Minnesota</option>
								<option value="MS">Mississippi</option>
								<option value="MO">Missouri</option>
								<option value="MT">Montana</option>
								<option value="NE">Nebraska</option>
								<option value="NV">Nevada</option>
								<option value="NH">New Hampshire</option>
								<option value="NJ">New Jersey</option>
								<option value="NM">New Mexico</option>
								<option value="NY">New York</option>
								<option value="NC">North Carolina</option>
								<option value="ND">North Dakota</option>
								<option value="OH">Ohio</option>
								<option value="OK">Oklahoma</option>
								<option value="OR">Oregon</option>
								<option value="PA" selected="selected">Pennsylvania</option>
								<option value="RI">Rhode Island</option>
								<option value="SC">South Carolina</option>
								<option value="SD">South Dakota</option>
								<option value="TN">Tennessee</option>
								<option value="TX">Texas</option>
								<option value="UT">Utah</option>
								<option value="VT">Vermont</option>
								<option value="VA">Virginia</option>
								<option value="WA">Washington</option>
								<option value="WV">West Virginia</option>
								<option value="WI">Wisconsin</option>
								<option value="WY">Wyoming</option>
							</select>
							<br>
							<label for="recordPurchasePropertyTypeID" class="input">Desired property type:</label>
							<select name="recordPurchasePropertyTypeID" id="recordPurchasePropertyTypeID" class="inputclass" title="Select a Property Type">
								<option value="">-Select-</option>
								<option value="1">Single Family Detached</option>
								<option value="2">Condo</option>
								<option value="3">Townhouse</option>
								<option value="4">Rental</option>
								<option value="5">Multi-Family</option>
								<option value="6">Vacation Home</option>
								<option value="7">Other</option>
							</select>
							<br>
							<div class="buttonWrapper">
								<input name="formNext1" type="button" class="open1 nextbutton" value="Next" alt="Next" title="Next">
							</div>
						</fieldset>
					</div>
				</li>
				<li id="sf2">
					<a href='#' class="ui-accordion-link">
					</a>
					<div>
						<fieldset>
							<legend>Step 2 of 3</legend>
							<div class="requiredNotice">*Required Field</div>
							<h3 class="stepHeader">Tell us about the property you're selling</h3>
							<label for="recordClientTimeFrameID" class="input required">When would you like to sell?</label>
							<select name="recordClientTimeFrameID" id="recordClientTimeFrameID" class="inputclass pageRequired" title="Select a Time Frame">
								<option value="">-Select-</option>
								<option value="1">Less than 3 months</option>
								<option value="2">3-6 months</option>
								<option value="3">6-9 months</option>
								<option value="4">9-12 months</option>
								<option value="5">Over 12 months</option>
							</select>
							<br>
							<label for="recordClientHomeTypeID" class="input required">Type of property you are selling:</label>
							<select name="recordClientHomeTypeID" id="recordClientHomeTypeID" class="inputclass pageRequired" title="Select a Property Type">
								<option value="">-Select-</option>
								<option value="1">Single Family Detached</option>
								<option value="2">Condo</option>
								<option value="3">Townhouse</option>
								<option value="4">Rental</option>
								<option value="5">Multi-Family</option>
								<option value="6">Vacation Home</option>
								<option value="7">Other</option>
							</select>
							<br>
							<label for="recordPropertyAddress1" class="input required">Property Street Address:</label>
							<input name="recordPropertyAddress1" id="recordPropertyAddress1" class="inputclass pageRequired" title="Street Address is required" maxlength="254" onblur="recordClientAddress1.value = this.value">
							<br>
							<label for="recordPropertyAddress2" class="input">Address (2):</label>
							<input name="recordPropertyAddress2" id="recordPropertyAddress2" class="inputclass" maxlength="254" onblur="recordClientAddress2.value = this.value">
							<br>
							<label for="recordPropertyCity" class="input required">City:</label>
							<input name="recordPropertyCity" id="recordPropertyCity" class="inputclass pageRequired" title="City is required" maxlength="254" onblur="recordClientCity.value = this.value">
							<br>
							<label for="recordPropertyState" class="input required">State:</label>
							<select name="recordPropertyState" id="recordPropertyState" class="inputclass pageRequired" title="Select a State" onchange="recordClientState.value = this.value">
								<option value="">-Select-</option>
								<option value="AL">Alabama</option>
								<option value="AK">Alaska</option>
								<option value="AZ">Arizona</option>
								<option value="AR">Arkansas</option>
								<option value="CA">California</option>
								<option value="CO">Colorado</option>
								<option value="CT">Connecticut</option>
								<option value="DE">Delaware</option>
								<option value="DC">Dist of Columbia</option>
								<option value="FL">Florida</option>
								<option value="GA">Georgia</option>
								<option value="HI">Hawaii</option>
								<option value="ID">Idaho</option>
								<option value="IL">Illinois</option>
								<option value="IN">Indiana</option>
								<option value="IA">Iowa</option>
								<option value="KS">Kansas</option>
								<option value="KY">Kentucky</option>
								<option value="LA">Louisiana</option>
								<option value="ME">Maine</option>
								<option value="MD">Maryland</option>
								<option value="MA">Massachusetts</option>
								<option value="MI">Michigan</option>
								<option value="MN">Minnesota</option>
								<option value="MS">Mississippi</option>
								<option value="MO">Missouri</option>
								<option value="MT">Montana</option>
								<option value="NE">Nebraska</option>
								<option value="NV">Nevada</option>
								<option value="NH">New Hampshire</option>
								<option value="NJ">New Jersey</option>
								<option value="NM">New Mexico</option>
								<option value="NY">New York</option>
								<option value="NC">North Carolina</option>
								<option value="ND">North Dakota</option>
								<option value="OH">Ohio</option>
								<option value="OK">Oklahoma</option>
								<option value="OR">Oregon</option>
								<option value="PA" selected="selected">Pennsylvania</option>
								<option value="RI">Rhode Island</option>
								<option value="SC">South Carolina</option>
								<option value="SD">South Dakota</option>
								<option value="TN">Tennessee</option>
								<option value="TX">Texas</option>
								<option value="UT">Utah</option>
								<option value="VT">Vermont</option>
								<option value="VA">Virginia</option>
								<option value="WA">Washington</option>
								<option value="WV">West Virginia</option>
								<option value="WI">Wisconsin</option>
								<option value="WY">Wyoming</option>
							</select>
							<br>
							<label for="recordPropertyZip" class="input required">Zip:</label>
							<input name="recordPropertyZip" id="recordPropertyZip" class="inputclass pageRequired" title="Zip Code is required" maxlength="254" onblur="recordClientZip.value = this.value">
							<br>
							<label for="recordClientPropertyValueID" class="input required">Estimated Market Value:</label>
							<select name="recordClientPropertyValueID" id="recordClientPropertyValueID" class="inputclass pageRequired" title="Select a Price Range">
								<option value="">-Select-</option>
								<option value="1">Less Than $75K</option>
								<option value="2">$75-$100K</option>
								<option value="3">$100-$125K</option>
								<option value="4">$125-$150K</option>
								<option value="5">$150-$200K</option>
								<option value="6">$200-$250K</option>
								<option value="7">$250-$300K</option>
								<option value="8">$300-$350K</option>
								<option value="9">$350-$400K</option>
								<option value="10">$400-$500K</option>
								<option value="11">$500-$700K</option>
								<option value="12">$700-$900K</option>
								<option value="13">Over $900K</option>
							</select>
							<br>
							<label for="recordPropertyBedroomsID" class="input">Bedrooms:</label>
							<select name="recordPropertyBedroomsID" id="recordPropertyBedroomsID" class="inputclass">
								<option value="">-Select-</option>
								<option value="1">1</option>
								<option value="2">2</option>
								<option value="3">3</option>
								<option value="4">4</option>
								<option value="5">5+</option>
							</select>
							<br>
							<label for="recordPropertyBathroomsId" class="input">Bathrooms:</label>
							<select name="recordPropertyBathroomsId" id="recordPropertyBathroomsId" class="inputclass">
								<option value="">-Select-</option>
								<option value="1">1</option>
								<option value="2">1.5</option>
								<option value="3">2</option>
								<option value="4">2.5</option>
								<option value="5">3</option>
								<option value="6">3.5</option>
								<option value="7">4+</option>
							</select>
							<br>
							<label for="recordPropertyAgeId" class="input">Approx. Age of Home:</label>
							<select name="recordPropertyAgeId" id="recordPropertyAgeId" class="inputclass">
								<option value="">-Select-</option>
								<option value="1">Less Than 1 year</option>
								<option value="2">1-5 years</option>
								<option value="3">6-10 years</option>
								<option value="4">11-15 years</option>
								<option value="5">More than 15 years</option>
							</select>
							<br>
							<label for="recordPropertySqFt" class="input">Approx. Square Footage:</label>
							<input name="recordPropertySqFt" id="recordPropertySqFt" class="inputclass" maxlength="254">
							<br>
							<div class="buttonWrapper">
								<input name="formBack0" type="button" class="open0 prevbutton" value="Back" alt="Back" title="Back">
								<input name="formNext2" type="button" class="open2 nextbutton" value="Next" alt="Next" title="Next">
							</div>
						</fieldset>
					</div>
				</li>
				<li id="sf3">
					<a href='#' class="ui-accordion-link">
					</a>
					<div>
						<fieldset>
							<legend>Step 3 of 3</legend>
							<div class="requiredNotice">*Required Field</div>
							<h3 class="stepHeader">Tell us about yourself</h3>
							<label for="recordClientNameFirst" class="input required">First Name:</label>
							<input name="recordClientNameFirst" id="recordClientNameFirst" class="inputclass pageRequired" title="First Name is required" maxlength="254">
							<br>
							<label for="recordClientNameLast" class="input required">Last Name:</label>
							<input name="recordClientNameLast" id="recordClientNameLast" class="inputclass pageRequired" maxlength="254" title="Last Name is required">
							<br>
							<label for="recordClientAddress1" class="input required">Current Address:</label>
							<input name="recordClientAddress1" id="recordClientAddress1" class="inputclass pageRequired" maxlength="254" title="Address is required">
							<br>
							<label for="recordClientAddress2" class="input">Address (2):</label>
							<input name="recordClientAddress2" id="recordClientAddress2" class="inputclass" maxlength="254">
							<br>
							<label for="recordClientCity" class="input required">City:</label>
							<input name="recordClientCity" id="recordClientCity" class="inputclass pageRequired" maxlength="254" title="City is required">
							<br>
							<label for="recordClientState" class="input required">State:</label>
							<select name="recordClientState" id="recordClientState" class="inputclass pageRequired" title="Select a State">
								<option value="">-Select-</option>
								<option value="AL">Alabama</option>
								<option value="AK">Alaska</option>
								<option value="AZ">Arizona</option>
								<option value="AR">Arkansas</option>
								<option value="CA">California</option>
								<option value="CO">Colorado</option>
								<option value="CT">Connecticut</option>
								<option value="DE">Delaware</option>
								<option value="DC">Dist of Columbia</option>
								<option value="FL">Florida</option>
								<option value="GA">Georgia</option>
								<option value="HI">Hawaii</option>
								<option value="ID">Idaho</option>
								<option value="IL">Illinois</option>
								<option value="IN">Indiana</option>
								<option value="IA">Iowa</option>
								<option value="KS">Kansas</option>
								<option value="KY">Kentucky</option>
								<option value="LA">Louisiana</option>
								<option value="ME">Maine</option>
								<option value="MD">Maryland</option>
								<option value="MA">Massachusetts</option>
								<option value="MI">Michigan</option>
								<option value="MN">Minnesota</option>
								<option value="MS">Mississippi</option>
								<option value="MO">Missouri</option>
								<option value="MT">Montana</option>
								<option value="NE">Nebraska</option>
								<option value="NV">Nevada</option>
								<option value="NH">New Hampshire</option>
								<option value="NJ">New Jersey</option>
								<option value="NM">New Mexico</option>
								<option value="NY">New York</option>
								<option value="NC">North Carolina</option>
								<option value="ND">North Dakota</option>
								<option value="OH">Ohio</option>
								<option value="OK">Oklahoma</option>
								<option value="OR">Oregon</option>
								<option value="PA" selected="selected">Pennsylvania</option>
								<option value="RI">Rhode Island</option>
								<option value="SC">South Carolina</option>
								<option value="SD">South Dakota</option>
								<option value="TN">Tennessee</option>
								<option value="TX">Texas</option>
								<option value="UT">Utah</option>
								<option value="VT">Vermont</option>
								<option value="VA">Virginia</option>
								<option value="WA">Washington</option>
								<option value="WV">West Virginia</option>
								<option value="WI">Wisconsin</option>
								<option value="WY">Wyoming</option>
							</select>
							<br>
							<label for="recordClientZip" class="input required">Zip:</label>
							<input name="recordClientZip" id="recordClientZip" class="inputclass pageRequired" maxlength="12" title="Zip Code is required">
							<br>
							<label for="recordClientPhone" class="input required">Phone Number:</label>
							<input name="recordClientPhone" id="recordClientPhone" class="inputclass pageRequired" maxlength="254" title="Phone Number is required">
							<br>
							<label for="recordClientPhoneAlt" class="input">Alternate Number:</label>
							<input name="recordClientPhoneAlt" id="recordClientPhoneAlt" class="inputclass" maxlength="254">
							<br>
							<label for="recordClientEmail" class="input required">Email Address:</label>
							<input name="recordClientEmail" id="recordClientEmail" class="inputclass pageRequired email" maxlength="254" title="Email address is required">
							<br>
							<label for="recordClientEmail1" class="input required">Confirm Email:</label>
							<input name="recordClientEmail1" id="recordClientEmail1" class="inputclass pageRequired" equalTo: "'#recordClientEmail" maxlength="254" title="Please confirm your email address">
							<br>
							<br>
							<p class="formDisclaimer">This is a sample form, no information is sent anywhere.</p>
							<div class="buttonWrapper">
								<input name="formBack1" type="button" class="open1 prevbutton" value="Back" alt="Back" title="Back">
								<input name="submit" type="submit" id="submit" value="Submit" class="submitbutton" alt="Submit" title="Submit">
							</div>
						</fieldset>
					</div>
				</li>
			</ul>
		</form>
	</div>
</div>
</body>
</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值