码迷,mamicode.com
首页 > 其他好文 > 详细

判断表单文本框(最后点击按钮才判断)

时间:2015-12-06 19:06:26      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
label {
display: inline-block;
width: 80px;
text-align: right;
}
span {
color: red;
}
</style>
</head>
<body>
<!-- onsubmit的默认事件是触发action -->
<form action="hi.html" onsubmit="">
<p><label for="username">用户名:</label><input type="text" id="username" /><span id="usernameWarn"></span></p>
<p><label for="password">密码:</label><input type="password" id="password" /><span id="passwordWarn"></span></p>
<p><label for="repassword">确认密码:</label><input type="password" id="repassword" /><span id="repasswordWarn"></span></p>
<!-- button的点击事件的默认事件是触发form表单的onsubmit -->
<p><button onclick="registerValidate()">注册</button></p>
</form>
</body>
<script>
var usernameEle = document.getElementById("username");
var usernameWarnEle = document.getElementById("usernameWarn");
var usernameRegExp = /^(\w+@\w+\.\w+)|(1[3458]\d{9})$/;

var passwordEle = document.getElementById("password");
var passwordWarnEle = document.getElementById("passwordWarn");
var passwordRegExp = /^[0-9a-zA-Z]{6,12}$/;

var repasswordEle = document.getElementById("repassword");
var repasswordWarnEle = document.getElementById("repasswordWarn");

function registerValidate() { //清空所有验证 usernameWarnEle.innerHTML = ""; passwordWarnEle.innerHTML = ""; repasswordWarnEle.innerHTML = ""; //验证用户名 if (usernameEle.value == "") { usernameWarnEle.innerHTML = "用户名不能为空!"; } else if (!usernameRegExp.test(usernameEle.value)) { usernameWarnEle.innerHTML = "用户名只能是邮箱或手机号码!"; } //验证密码 if (passwordEle.value == "") { passwordWarnEle.innerHTML = "密码不能为空!"; } else if (!passwordRegExp.test(passwordEle.value)) { passwordWarnEle.innerHTML = "密码只能由6到12位的英文字母或数字组成!"; } //确认密码 if (repasswordEle.value == "") { repasswordWarnEle.innerHTML = "密码不能为空!"; } else if (passwordEle.value != repasswordEle.value) { repasswordWarnEle.innerHTML = "输入的密码不一致!"; } }</script></html>

判断表单文本框(最后点击按钮才判断)

标签:

原文地址:http://www.cnblogs.com/Shirley-He/p/5023936.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!