侧边栏壁纸
  • 累计撰写 781 篇文章
  • 累计创建 1 个标签
  • 累计收到 1 条评论
标签搜索

H5 输入验证

Dettan
2021-04-10 / 0 评论 / 0 点赞 / 119 阅读 / 7,879 字
温馨提示:
本文最后更新于 2022-04-30,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

要用 oninput() 才能在符合规则后自动消失.
document.getElementById("rePassword").setCustomValidity("两次输入的密码不一致!");
这是一个跟form联动的输入验证提示. 这一句作用就是在点击提交的时候对应输入框会显示这个提示同时不会提交,设为空之后就会提交.
<form action="insertUser.do" method="post">
	<div class="form-sub-w3">
		<input type="text" name="userName" placeholder="姓名" required=""/>
	</div>
	<div class="form-sub-w3">
		<input type="text" id ="phone" name="userTele" placeholder="手机号" required="" pattern="^.{6,}$" required />
	</div>
	<div class="form-sub-w3">
		<input type="password" id="password" name="userPwd" placeholder="密码" pattern="^.{6,}$" required="" title="至少六位"/>
	</div>
	<div class="form-sub-w3">
		<input type="password" id="rePassword" name="userPwd" placeholder="确认密码" required=""/>
	</div>
	<div class="form-sub-w3">
		<input type="text" name="userLoc" placeholder="地址" required=""/>
	</div>
	<div class="submit-agileits">
		<input type="submit" value="Reg">
	</div>
		<a href="userlogin.jsp">登录!!</a>
</form>

<script type="application/javascript">
document.getElementById("phone").oninvalid = function () {
document.getElementById("phone").setCustomValidity("请输入正确的手机号!");
};
document.getElementById("phone").oninput = function () {
document.getElementById("phone").setCustomValidity("");
};
document.getElementById("rePassword").oninput = function () {
if (document.getElementById("password").value !== document.getElementById("rePassword").value)
document.getElementById("rePassword").setCustomValidity("两次输入的密码不一致!");
else
document.getElementById("rePassword").setCustomValidity("");
};
</script>





//数据库异常获取
}catch (Exception $e){

if (10501 == $e->getCode()) {
if (strpos($e->getMessage(), 'Duplicate entry')) //主键冲突
return json_encode(array('type' => 'register', 'code' => 0, 'message' => '学号或者邮箱已被使用!'));
else if (strpos($e->getMessage(), '23000')) //由于外键约束未插入成功
return json_encode(array('type' => 'register', 'code' => 0, 'message' => '学校或专业错误!'. $e->getMessage()));
else
return json_encode(array('type' => 'register', 'code' => 0, 'message' => '未知数据错误!' . $e->getCode()));
} else {
return json_encode(array('type' => 'register', 'code' => 0, 'message' => '系统错误' . $e->getMessage()));
}

}

//本地js验证
form = $('#form');
form.validate(myValid);

function submit_login(ele) {
var err=form.valid();
if(!err){
return 0;
}
$.ajax({
url: "{:url('admin/login/login')}",
type: "post",
data: form.serialize(),
success: function (data) {
data = JSON.parse(data);
if('login' == data['type'] && 0 == data['code']){
refreshImg();
$("#captcha").val("");
}
progressMessage(data);
}
});
}

//php
$valid = controller('Common/Valid');
if (!$valid->Valid($_POST))
return json_encode(array('type' => 'stuVali', 'code' => 0, 'message' => $valid->error));

复选框是否选中:
$("#option-institute").is(":checked")

核心代码:
$("#submit").click(function(){
alert($("#orderform").valid());
});

把网址下的代码粘贴在下面网址里看效果
https://www.runoob.com/try/try.php?filename=jquery-plugin-dynamic-totals

<!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="//static.runoob.com/assets/jquery-validation-1.14.0/demo/css/screen.css">
<script src="https://static.runoob.com/assets/jquery-validation-1.14.0/lib/jquery.js"></script>
<script src="https://static.runoob.com/assets/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script>
<script src="https://static.runoob.com/assets/jquery-validation-1.14.0/dist/localization/messages_zh.js"></script>

<script type="text/javascript">
// 只用于演示
$.validator.setDefaults({
submitHandler: function() {
alert("submitted!");
}
});
$.validator.messages.max = jQuery.validator.format("Your totals mustn't exceed {0}!");

$.validator.addMethod("quantity", function(value, element) {
return !this.optional(element) && !this.optional($(element).parent().prev().children("select")[0]);
}, "请选择项目和数量。");

$().ready(function() {
$("#orderform").validate({
errorPlacement: function(error, element) {
error.appendTo( element.parent().next() );
},
highlight: function(element, errorClass) {
$(element).addClass(errorClass).parent().prev().children("select").addClass(errorClass);
}
});

var template = jQuery.validator.format($.trim($("#template").val()));
function addRow() {
$(template(i++)).appendTo("#orderitems tbody");
}

var i = 1;
// 开始时只有一行
addRow();
// 点击时添加更多行
$("#add").click(addRow);
$("#submit").click(function(){
alert($("#orderform").valid());

});

// 检查 quantity 输入框的 keyup 动作,更新 totals 字段
$("#orderform").validateDelegate("input.quantity", "keyup", function(event) {
var totals = 0;
$("#orderitems input.quantity").each(function() {
totals += +this.value;
});
$("#totals").attr("value", totals).valid();
});

});
</script>

<style type="text/css">
form.cmxform { width: 50em; }
em.error {
background:url("/try/jquery/plugins/images/unchecked.gif") no-repeat 0px 0px;
padding-left: 16px;
}
em.success {
background:url("/try/jquery/plugins/images/checked.gif") no-repeat 0px 0px;
padding-left: 16px;
}

form.cmxform label.error {
margin-left: auto;
width: 250px;
}
form.cmxform input.submit {
margin-left: 0;
}
em.error { color: black; }
#warning { display: none; }
select.error {
border: 1px dotted red;
}
</style>

</head>
<body>

<div id="main">

<textarea style="display:none" id="template">
<tr>
<td>
<label>{0}. Item</label>
</td>
<td class='type'>
<select name="item-type-{0}">
<option value="">请选择...</option>
<option value="0">Learning jQuery</option>
<option value="1">jQuery Reference Guide</option>
<option value="2">jQuery Cookbook</option>
<option vlaue="3">jQuery In Action</option>
<option value="4">jQuery For Designers</option>
</select>
</td>
<td class='quantity'>
<input size='4' class="quantity" min="1" id="item-quantity-{0}" name="item-quantity-{0}" />
</td>
<td class='quantity-error'></td>
</tr>
</textarea>

<form id="orderform" class="cmxform" method="get" action="foo.html">
<h2 id="summary"></h2>

<fieldset>
<legend>动态表单</legend>
<table id="orderitems">
<tbody>

</tbody>
<tfoot>
<tr>
<td colspan="2"><label>总共(最多 25)</label></td>
<td class="totals"><input id="totals" name="totals" value="0" max="25" readonly="readonly" size='4' /></td>
<td class="totals-error"></td>
</tr>
<tr>
<td colspan="2"> </td>
<td></td>
</tr>
</tfoot>
</table>
</fieldset>
</form>

<button id="add">向表单添加输入框</button>

<h1 id="warning">您的表单包含大量的错误!请重新输入。</h1>
<button id="submit">提交</button>
</div>

</body>
</html>

0

评论区