<body>
<form id="form1" runat="server">
<asp:TextBox ID="txtInt" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ClientValidationFunction="validate_Integer" Display="Dynamic"
ErrorMessage="<img src='Images/check_error.gif' style='13px;height:13px;'>" >
</asp:CustomValidator><br />
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
<form id="form1" runat="server">
<asp:TextBox ID="txtInt" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ClientValidationFunction="validate_Integer" Display="Dynamic"
ErrorMessage="<img src='Images/check_error.gif' style='13px;height:13px;'>" >
</asp:CustomValidator><br />
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
其中调用javascript函数validate_Integer,所以在<head></head>之间插入脚本代码
<script type="text/javascript" language="javascript">
function isInteger(s)
{
var i;
for (i = 0; i < s.length; i++)
{ //检测字符是否为数字
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
return true;
}
function validate_Integer(source, args)
{
var txtintval=document.getElementById("<%= txtInt.ClientID %>");
if(!isInteger (txtintval.value))
{
args.IsValid=false;
}
else
{
args.IsValid=true;
}
}
</script>
function isInteger(s)
{
var i;
for (i = 0; i < s.length; i++)
{ //检测字符是否为数字
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
return true;
}
function validate_Integer(source, args)
{
var txtintval=document.getElementById("<%= txtInt.ClientID %>");
if(!isInteger (txtintval.value))
{
args.IsValid=false;
}
else
{
args.IsValid=true;
}
}
</script>
二、使用RequiredFieldValidator进行非空验证
1、传统的验证方式
通过设置其ErrorMessage属性指定验证错误提示信息
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" EnableClientScript="False" ErrorMessage="*输入字段不能为空!" >
</asp:RequiredFieldValidator>
2.弹出JavaScript的提示窗口<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" EnableClientScript="False" ErrorMessage="*输入字段不能为空!" >
</asp:RequiredFieldValidator>
只需要将验证控件的Text属性赋上一句JavaScript代码
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox2" EnableClientScript="False" ErrorMessage="RequiredFieldValidator"
Text="<script type='text/javascript'>alert('*输入字段不能为空!')</script>" >
</asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox2" EnableClientScript="False" ErrorMessage="RequiredFieldValidator"
Text="<script type='text/javascript'>alert('*输入字段不能为空!')</script>" >
</asp:RequiredFieldValidator>
3.带声音提示的验证,此时也可以用图片做为错误提示信息
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="TextBox3"
EnableClientScript="False"
ErrorMessage="RequiredFieldValidator"
Text="<bgsound src='demo1.wav'>">
</asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
</body>
来自:http://www.cnblogs.com/beniao/archive/2008/07/06/1236330.html