js验证提交
<script type="text/javascript">
var kong = /^$/;
var flag = false;
function lnTest() {
var ur_loginName = document.getElementById("ur_loginName").value;
var login = document.getElementById("login");
var id = "${sessionScope.user.ur_id}";
var ymsz = /^[A-Za-z0-9]+$/;
var loginname = document.getElementById("loginname").value;
if(loginname==ur_loginName){
login.style.color = "green";
login.innerHTML = " √ 该用户名可以使用";
return true;
}else if (kong.test(ur_loginName)) {
login.style.color = "red";
login.innerHTML = " × 账户不可以为空";
flag = false;
} else if (ymsz.test(ur_loginName)) {
xml = new XMLHttpRequest();
xml.open("get", "AJAXServlet.do", true);
xml.setRequestHeader("name", ur_loginName);
xml.setRequestHeader("ur_id", id);
xml.send(null);
xml.onreadystatechange = function(){
if (xml.readyState == 4) {
var msg = xml.responseText;
var login = document.getElementById("login");
if (msg == 1) {
login.style.color = "red";
login.innerHTML = " × 该用户名已被使用";
flag = false;
return false;
} else if (msg != 1) {
login.style.color = "green";
login.innerHTML = " √ 该用户名可以使用";
flag = true;
return true;
}
}
};
} else {
login.style.color = "red";
login.innerHTML = " × 账户格式不正确 ";
flag = false;
}
if (flag == true) {
return true;
} else {
return false;
}
}
function IDTest() {
var ur_idcard = document.getElementById("ur_idcard").value;
var id = document.getElementById("id");
var idy =/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
if (kong.test(ur_idcard)) {
id.style.color = "red";
id.innerHTML = " × 身份证不可以为空";
return false;
} else if (idy.test(ur_idcard)) {
id.style.color = "green";
id.innerHTML = " √ ";
SexTest();
return true;
} else {
id.style.color = "red";
id.innerHTML = " × 身份证格式不正确 ";
return false;
}
}
function SexTest() {
var ur_idcard = document.getElementById("ur_idcard").value;
var nan = document.getElementById("nan");
var nv = document.getElementById("nv");
var arr = ur_idcard.replace(/(.)(?=[^$])/g, "$1,").split(",");
if (arr[arr.length - 2] % 2 == 0) {
/* nv.checked="checked";
nan.checked=""; */
nv.checked = true;
nan.checked = false;
} else {
nv.checked = false;
nan.checked = true;
}
}
function nmTest() {
var ur_name = document.getElementById("ur_name").value;
var nam = document.getElementById("nam");
var hanzi = /^[\u4e00-\u9fa5]{0,4}$/;
if (kong.test(ur_name)) {
nam.style.color = "red";
nam.innerHTML = " × 真实姓名不可以为空";
return false;
} else if (hanzi.test(ur_name)) {
nam.style.color = "green";
nam.innerHTML = " √ ";
return true;
} else {
nam.style.color = "red";
nam.innerHTML = " × 真实姓名格式不正确 ";
return false;
}
}
function peTest() {
var ur_phone = document.getElementById("ur_phone").value;
var phon = document.getElementById("phon");
var dh = /^1[0-9]{10}$/;
if (kong.test(ur_phone)) {
phon.style.color = "red";
phon.innerHTML = " × 电话不可以为空";
return false;
} else if (dh.test(ur_phone)) {
phon.style.color = "green";
phon.innerHTML = " √ ";
return true;
} else {
phon.style.color = "red";
phon.innerHTML = " × 电话格式不正确 ";
return false;
}
}
function elTest() {
var ur_email = document.getElementById("ur_email").value;
var emai = document.getElementById("emai");
var em = /^[A-Za-z0-9]+([._\\-]*[A-Za-z0-9])*@([A-Za-z0-9]+[-A-Za-z0-9]*[A-Za-z0-9]+.){1,63}[A-Za-z0-9]+$/;
if (kong.test(ur_email)) {
emai.style.color = "red";
emai.innerHTML = " × 邮箱不可以为空";
return false;
} else if (em.test(ur_email)) {
emai.style.color = "green";
emai.innerHTML = " √ ";
return true;
} else {
emai.style.color = "red";
emai.innerHTML = " × 邮箱格式不正确 ";
return false;
}
}
function adTest() {
var ur_address = document.getElementById("ur_address").value;
var addr = document.getElementById("addr");
if (kong.test(ur_address)) {
addr.style.color = "red";
addr.innerHTML = " × 地址不可以为空";
return false;
} else {
addr.style.color = "green";
addr.innerHTML = " √ ";
return true;
}
}
function subTest() {
var isok = lnTest();
var isok1 = nmTest();
var isok2 = IDTest();
var isok3 = peTest();
var isok4 = elTest();
var isok5 = adTest();
alert(isok);
/* alert(isok1);
alert(isok2);
alert(isok3);
alert(isok4);
alert(isok5); */
if (isok && isok1 && isok2 && isok3 && isok4 && isok5) {
if (confirm("保存信息?")) {
document.getElementById("form").submit();
} else {
return false;
}
} else {
alert("请全部填写正确后提交");
return false;
}
}
</script>
使用地点:
<TR>
<th width="20%" align="center">用户登录名</th>
<TD class="BGCgray"><input name="ur_loginName" type="text"
class="inputbox" id="ur_loginName"
style="width:160px;hight:20px"
value="${sessionScope.user.ur_loginName }" onblur="lnTest()"><span
id="login" style="color:red"></span></TD>
</TR>
onblur是js的函数名,<span>标签是 addr.innerHTML眼显示的内容