87,902
社区成员
发帖
与我相关
我的任务
分享
<script language="javascript" type="text/javascript">
function check()
{
isNone("username","用户名有能为空"); //不要用return
isNone("postcode","邮编不能为空");
isNone("handset","手机不能为空");
}
function isNone = function (txtID,tips)
{
var txt = document.getElementById(txtID);
if (txt.value == "")
{
window.alert(tips);
//这个地方如何取消form的提交?
}
}
<script>
//其它函数
String.prototype.trim = function ()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
function $(objID) //根据ID返回对象
{
return document.getElementById(objID);
}
function isIE() //检测浏览器类型
{
if (window.ActiveXObject)
return true;
else
return false;
}
function checkForm()
{
this.eventObj;
this.flag = true;
this.txtObj;
this.tipStr;
this.getTextObject = function (txtID,tips) //参数说明:txtID 文本框ID,tips 错误提示信息
{
this.txtObj = $(txtID);
this.tipStr = tips;
}
this.errorTip = function () //错误提示
{
window.alert(this.tipStr);
this.txtObj.focus();
if (isIE())
this.eventObj.returnValue = false;
else
this.eventObj.preventDefault();
}
this.isNone = function (txtID,tips) //空值检测
{
this.getTextObject(txtID,tips);
if (this.txtObj.value.trim().length == 0 && this.flag)
{
this.flag = false;
this.errorTip();
}
}
}
<form id="soform" name="soform" method="post" action="Count.asp" onsubmit="cf(event);">
<input type="text" id="username" name="username" />用户名<br />
<input type="text" id="postcode" name="postcode" />邮编<br />
<input type="text" id="handset" name="handset" />手机<br />
<input type="text" id="idcard" name="idcard" />身份证<br />
<input type="text" id="qq" name="qq" />QQ<br />
<input type="text" id="email" name="email" />email<br />
<input type="submit" value="提交" />
<input type="reset" value="重置" />
</form>
<script language="javascript" type="text/javascript">
var c = new checkForm();
function cf(eventObject)
{
c.eventObj = eventObject;
c.flag = true;
c.isNone("username","用户名不能为空");
c.isNone("postcode","邮编不能为空");
c.isNone("handset","手机不能为空");
}
</script>
//其它函数
String.prototype.trim = function ()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
function $(objID) //根据ID返回对象
{
return document.getElementById(objID);
}
function checkForm()
{
this.flag = true;
this.txtObj;
this.tipStr;
this.getTextObject = function (txtID,tips) //参数说明:txtID 文本框ID,tips 错误提示信息
{
this.txtObj = $(txtID);
this.tipStr = tips;
}
this.errorTip = function () //错误提示
{
window.alert(this.tipStr);
this.txtObj.focus();
event.returnValue = false;
event.cancel = true;
}
this.isNone = function (txtID,tips) //空值检测
{
this.getTextObject(txtID,tips);
if (this.txtObj.value.trim().length == 0 && this.flag)
{
this.flag = false;
this.errorTip();
}
}
}
<form id="soform" name="soform" method="post" action="Count.asp" onsubmit="cf();">
<input type="text" id="username" name="username" />用户名<br />
<input type="text" id="postcode" name="postcode" />邮编<br />
<input type="text" id="handset" name="handset" />手机<br />
<input type="text" id="idcard" name="idcard" />身份证<br />
<input type="text" id="qq" name="qq" />QQ<br />
<input type="text" id="email" name="email" />email<br />
<input type="submit" value="提交" />
<input type="reset" value="重置" />
</form>
<script language="javascript" type="text/javascript">
var c = new checkForm();
function cf()
{
c.flag = true;
c.isNone("username","用户名不能为空");
c.isNone("postcode","邮编不能为空");
c.isNone("handset","手机不能为空");
}
</script>
<html>
<head>
<script type="text/javascript">
var flag=1;
function check()
{
isNone("username","用户名有能为空");
isNone("postcode","邮编不能为空");
isNone("handset","手机不能为空");
}
function isNone(txtID,tips)
{
var txt = document.getElementById(txtID);
if(flag==1) {
if (txt.value == "")
{ flag=0;
window.alert(tips);
event.returnValue = false;
event.cancel = true;
}
}
}
</script>
</head>
<body>
<form id="soform" name="soform" method="post" action="Count.asp" onsubmit="check();">
<input type="text" id="username" name="username" />用户名 <br />
<input type="text" id="postcode" name="postcode" />邮编 <br />
<input type="text" id="handset" name="handset" />手机 <br />
<input type="submit" value="提交" />
<input type="reset" value="重置" />
</form>
</body>
</html>