JavaScriptのフォーム検証(フォーム検証器のパッケージ)
3340 ワード
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk"/>
<title>Copy of formReg</title>
<script src="util/util.js">
</script>
<script>
// validForm()
function validForm(EleName,EleSpanName,NullMsg,ErrMsg,RegexInfo) {
var flag = true;
var username = $(EleName).value.trim();
if (!username) {
$(EleSpanName).innerHTML=NullMsg;
flag = false;
}
else{
var unameRegex = RegexInfo;
if (!unameRegex.test(username)){
$(EleSpanName).innerHTML=ErrMsg;
flag = false;
}
}
return flag;
}
function validateForm(){
var flagOut=validForm("#uname","#unameSpan",
"\t !","\t 3-10!",
/^[\u4E00-\u9FA5]{3,8}$/ig);
flagOut=flagOut&&validForm("#email","#emailSpan",
"\t !","\t !",
/^\w+@[a-z1-9]{2,5}(\.[a-z]{2,3}){1,2}$/ig);
flagOut=flagOut&&validForm("#contact","#contactSpan",
"\t !","\t !",
/(^0\d{3}-\d{7}$)|(^0\d{2}-\d{8}$)|(^1[134578]\d{9}$)/ig);
return flagOut;
}
/*************************************************
// validateForm
function validateForm() {
// flag, true, ,
var flag = true;
// trim, trim
var username = $("#uname").value.trim();
// , innerHTML, “ !”,
if (!username) {
$("#unameSpan").innerHTML="\t !";
// flag false
flag = false;
}
// , , ,
// , , “ 3-10!”
// flag false
else{
var unameRegex = /^[\u4E00-\u9FA5]{3,8}$/ig;
if (!unameRegex.test(username)){
$("#unameSpan").innerHTML="\t 3-10!";
// flag false
flag = false;
}
}
// flag
return flag;
}
*************************************************/
</script>
</head>
<body>
<form action="#">
:<input id="uname" name="uname"/><span id="unameSpan"></span><br /><br />
:<input id="email" name="email"/><span id="emailSpan"></span><br /><br />
:<input id="contact" name="contact"/><span id="contactSpan"></span><br /><br />
<input type="submit" value=" " onclick="return validateForm();"/>
</form>
</body>
</html>