jQuery开发登录注册页面,要有验证

如题所述

在开发登录注册页面时,验证是非常重要的。以下是使用jQuery进行验证的示例代码:HTML部分:```\u003cform id=\"register-form\"\u003e \u003cdiv\u003e \u003clabel for=\"username\"\u003e用户名:\u003c/label\u003e \u003cinput type=\"text\" id=\"username\" name=\"username\"\u003e \u003c/div\u003e \u003cdiv\u003e \u003clabel for=\"password\"\u003e密码:\u003c/label\u003e \u003cinput type=\"password\" id=\"password\" name=\"password\"\u003e \u003c/div\u003e \u003cdiv\u003e \u003clabel for=\"email\"\u003e电子邮件:\u003c/label\u003e \u003cinput type=\"email\" id=\"email\" name=\"email\"\u003e \u003c/div\u003e \u003cdiv\u003e \u003clabel for=\"confirm-password\"\u003e确认密码:\u003c/label\u003e \u003cinput type=\"password\" id=\"confirm-password\" name=\"confirm-password\"\u003e \u003c/div\u003e \u003cdiv\u003e \u003cinput type=\"submit\" value=\"注册\"\u003e \u003c/div\u003e\u003c/form\u003e```jQuery部分:```$(document).ready(function() { $('#register-form').submit(function(event) { // 阻止表单提交 event.preventDefault(); // 获取表单数据 var username = $('#username').val(); var password = $('#password').val(); var email = $('#email').val(); var confirmPassword = $('#confirm-password').val(); // 进行验证 var isValid = true; if (!username) { alert('请输入用户名!'); isValid = false; } if (!password) { alert('请输入密码!'); isValid = false; } if (!email) { alert('请输入电子邮件!'); isValid = false; } if (password !== confirmPassword) { alert('密码与确认密码不一致!'); isValid = false; } // 如果验证通过,则提交表单 if (isValid) { $('#register-form')[0].submit(); } });});```以上代码首先使用jQuery的submit()函数监听表单的提交事件,在事件处理函数中阻止表单的默认提交行为。然后使用val()函数获取表单中输入的数据,并进行验证。如果存在验证失败的情况,则弹出提示信息。如果所有验证都通过,则使用submit()函数提交表单。需要注意的是,在验证密码和确认密码是否一致时,使用了“!==”运算符,而不是“==”运算符。这是因为在JavaScript中,“==”运算符会进行类型转换,而“!==”则不会。因此使用“!==”可以确保密码和确认密码的数据类型和值都相同。
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-06-20
jQuery开发登录注册页面,无非就是获取用户提交的表单,用jquery表单值进行正则匹配,如果匹配成功则用ajax提交数据给后台处理存入数据库。若正则不匹配则提示错误,禁止提交。本回答被网友采纳
第2个回答  2015-05-15
你好

提供一个范例 : http://www.formget.com/jquery-login-form/
另外一个范例代码:
<html>
<head>
<meta charset="utf-8">
<title>jQuery Validation Plugin Examples</title>

<!-- Bootstrap CSS used for styling the demo pages-->
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css">
</head>

<body>
<!-- basic validation of a login form -->
<form class="form" id="example1">
<div class="form-group">
<label>Basic login form example</label>
</div>
<div class="form-group">
<label for="example1-email">Email address</label>
<input type="email" class="form-control" id="example1-email" name="example1-email" placeholder="Enter email">
</div>
<div class="form-group">
<label for="example1-password">Password</label>
<input type="password" class="form-control" id="example1-password" name="example1-password" placeholder="Password">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>

<!-- Load jQuery and jQuery-Validate scripts -->
<script src="js/jquery.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script>
// When the document is ready
$(document).ready(function () {

//validation rules
//by default it will append a <label class="error> element to the invalid input
//and will add a "error" class to the input
$("#example1").validate({
rules: {
"example1-email": {
required: true,
email: true
},
"example1-password": {
required: true,
minlength: 5
}
}
});
});
</script>
</body>
</html>本回答被网友采纳
相似回答