.net中客户端验证可以用微软自带的验证控件,但明显没有直接写的javascript来得简单有效,请参照以下三步:
第一:form如下:
<form id=“form1“ method=“post“ runat=“server“>
<font face=“宋体“></font><font face=“宋体“></font>
<br>
1. name :<br>
<asp:textbox id=“txtname“ runat=“server“ /><br>
2. email :<br>
<asp:textbox id=“txtemail“ runat=“server“ /><br>
3. web url :<br>
<asp:textbox id=“txtweburl“ runat=“server“ /><br>
4. zip :<br>
<asp:textbox id=“txtzip“ runat=“server“ /><br>
5.content<br>
<asp:textbox id=“txtcontent“ runat=“server“ textmode=“multiline“ width=“504px“ height=“80px“ />
<br>
<asp:button id=“btnsubmit“ onclientclick=“ return validate()“ runat=“server“ text=“submit“ />
</form>
<font face=“宋体“></font><font face=“宋体“></font>
<br>
1. name :<br>
<asp:textbox id=“txtname“ runat=“server“ /><br>
2. email :<br>
<asp:textbox id=“txtemail“ runat=“server“ /><br>
3. web url :<br>
<asp:textbox id=“txtweburl“ runat=“server“ /><br>
4. zip :<br>
<asp:textbox id=“txtzip“ runat=“server“ /><br>
5.content<br>
<asp:textbox id=“txtcontent“ runat=“server“ textmode=“multiline“ width=“504px“ height=“80px“ />
<br>
<asp:button id=“btnsubmit“ onclientclick=“ return validate()“ runat=“server“ text=“submit“ />
</form>
第二:调用函数如下(可自行扩展)
<script language=“javascript“ type=“text/javascript“>
function validate()
{
if (document.getelementbyid(“<%=txtname.clientid%>“).value==“”)
{
alert(“name feild can not be blank“);
document.getelementbyid(“<%=txtname.clientid%>“).focus();
return false;
}
if(document.getelementbyid(“<%=txtemail.clientid %>“).value==“”)
{
alert(“email id can not be blank“);
document.getelementbyid(“<%=txtemail.clientid %>“).focus();
return false;
}
// var emailpat = /^(“.*”|[a-za-z]w*)@([d{1,3}(.d{1,3}){3}]|[a-za-z]w*(.[a-za-z]w*)+)$/;
//var emailpat = “w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*”;
//var emailpat = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
//var emailpat = ^([w]+@([w]+.)+[a-za-z]{2,9}(s*;s*[w]+@([w]+.)+[a-za-z]{2,9})*)$;
var emailpat = “^[a-za-z0-9_.]+@[a-za-z0-9-]+[.a-za-z]+$“;
var emailid=document.getelementbyid(“<%=txtemail.clientid %>“).value;
var matcharray = emailid.match(emailpat);
if (matcharray == null)
{
alert(“your email address seems incorrect. please try again.“);
document.getelementbyid(“<%=txtemail.clientid %>“).focus();
return false;
}
if(document.getelementbyid(“<%=txtweburl.clientid %>“).value==“”)
{
alert(“web url can not be blank“);
document.getelementbyid(“<%=txtweburl.clientid %>“).value=“http://“
document.getelementbyid(“<%=txtweburl.clientid %>“).focus();
return false;
}
var url=“^[a-za-z]+://[a-za-z0-9-_]+\.[a-za-z0-9-_%&?/.=]+$“
var tempurl=document.getelementbyid(“<%=txtweburl.clientid%>“).value;
var matchurl=tempurl.match(url);
if(matchurl==null)
{
alert(“web url does not look valid“);
document.getelementbyid(“<%=txtweburl.clientid %>“).focus();
return false;
}
if (document.getelementbyid(“<%=txtzip.clientid%>“).value==“”)
{
alert(“zip code is not valid“);
document.getelementbyid(“<%=txtzip.clientid%>“).focus();
return false;
}
var digits=“0123456789“;
var temp;
for (var i=0;i<document.getelementbyid(“<%=txtzip.clientid %>“).value.length;i++)
{
temp=document.getelementbyid(“<%=txtzip.clientid%>“).value.substring(i,i+1);
if (digits.indexof(temp)==-1)
{
alert(“please enter correct zip code“);
document.getelementbyid(“<%=txtzip.clientid%>“).focus();
return false;
}
}
var contentlength=“”;
if (document.getelementbyid(“<%=txtcontent.clientid%>“).value==“”)
{
alert(“content is need!“);
document.getelementbyid(“<%=txtcontent.clientid%>“).focus();
return false;
}
else if(document.getelementbyid(“<%=txtcontent.clientid%>“).value.length>100)
{
alert(“content is too long,and more than 100 chars!“);
document.getelementbyid(“<%=txtcontent.clientid%>“).focus();
return false;
}
return true;
}
</script>
function validate()
{
if (document.getelementbyid(“<%=txtname.clientid%>“).value==“”)
{
alert(“name feild can not be blank“);
document.getelementbyid(“<%=txtname.clientid%>“).focus();
return false;
}
if(document.getelementbyid(“<%=txtemail.clientid %>“).value==“”)
{
alert(“email id can not be blank“);
document.getelementbyid(“<%=txtemail.clientid %>“).focus();
return false;
}
// var emailpat = /^(“.*”|[a-za-z]w*)@([d{1,3}(.d{1,3}){3}]|[a-za-z]w*(.[a-za-z]w*)+)$/;
//var emailpat = “w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*”;
//var emailpat = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
//var emailpat = ^([w]+@([w]+.)+[a-za-z]{2,9}(s*;s*[w]+@([w]+.)+[a-za-z]{2,9})*)$;
var emailpat = “^[a-za-z0-9_.]+@[a-za-z0-9-]+[.a-za-z]+$“;
var emailid=document.getelementbyid(“<%=txtemail.clientid %>“).value;
var matcharray = emailid.match(emailpat);
if (matcharray == null)
{
alert(“your email address seems incorrect. please try again.“);
document.getelementbyid(“<%=txtemail.clientid %>“).focus();
return false;
}
if(document.getelementbyid(“<%=txtweburl.clientid %>“).value==“”)
{
alert(“web url can not be blank“);
document.getelementbyid(“<%=txtweburl.clientid %>“).value=“http://“
document.getelementbyid(“<%=txtweburl.clientid %>“).focus();
return false;
}
var url=“^[a-za-z]+://[a-za-z0-9-_]+\.[a-za-z0-9-_%&?/.=]+$“
var tempurl=document.getelementbyid(“<%=txtweburl.clientid%>“).value;
var matchurl=tempurl.match(url);
if(matchurl==null)
{
alert(“web url does not look valid“);
document.getelementbyid(“<%=txtweburl.clientid %>“).focus();
return false;
}
if (document.getelementbyid(“<%=txtzip.clientid%>“).value==“”)
{
alert(“zip code is not valid“);
document.getelementbyid(“<%=txtzip.clientid%>“).focus();
return false;
}
var digits=“0123456789“;
var temp;
for (var i=0;i<document.getelementbyid(“<%=txtzip.clientid %>“).value.length;i++)
{
temp=document.getelementbyid(“<%=txtzip.clientid%>“).value.substring(i,i+1);
if (digits.indexof(temp)==-1)
{
alert(“please enter correct zip code“);
document.getelementbyid(“<%=txtzip.clientid%>“).focus();
return false;
}
}
var contentlength=“”;
if (document.getelementbyid(“<%=txtcontent.clientid%>“).value==“”)
{
alert(“content is need!“);
document.getelementbyid(“<%=txtcontent.clientid%>“).focus();
return false;
}
else if(document.getelementbyid(“<%=txtcontent.clientid%>“).value.length>100)
{
alert(“content is too long,and more than 100 chars!“);
document.getelementbyid(“<%=txtcontent.clientid%>“).focus();
return false;
}
return true;
}
</script>
第三:page_load事件中加入一行:
private void page_load(object sender, system.eventargs e)
{
btnsubmit.attributes.add(“onclick”, “return validate()”);
}
{
btnsubmit.attributes.add(“onclick”, “return validate()”);
}