ASP.Net验证控件和Javascript
问题描述:
如何同时使用JavaScript和ASP.Net验证控件?
How to use both JavaScript and ASP.Net Validation controls in parallel ?
答
请在使用文本框验证的情况下找到以下代码,仅将带aspx文本框的javascript用作数字值.
Hi,
Please find the below code with textbox validation has only numeric value using javascript with aspx textbox.
//Javascript code:
<script type="text/javascript">
function ValidateCCNum(ccNum) {
var ccno = ccNum.value;
if (isNaN(ccno)) {
alert("Textbox value should be numeric");
ccNum.focus();
ccNum.value = null;
return false;
}
return true;
}
</script>
</head>
<![CDATA[<%--In Aspx file call this function as below--%>]]>
<asp:content runat="server" id="BodyContent" contentplaceholderid="MainContent" xmlns:asp="#unknown">
<asp:textbox id="txtCCDNo" onblur="ValidateCCNum(this)" runat="server"> </asp:textbox>
</asp:content>
在aspx客户端页面中运行以下代码
Run this code in aspx client page
JS:
<script type="text/javascript" language="javascript">
function validate(sender,args) {
args.IsValid = false;
var tb = document.getElementById('txtInput');
if (args.Value.match(/^\w{6}\d{6}
/)){ args.IsValid = true tb.style.backgroundColor =白色"; tb.style.color ="green"; } 别的 { tb.style.backgroundColor =红色"; } } < /script > aspx页面: < asp:TextBox ID =" runat 服务器" < > < asp:Button ID =" runat 服务器" 文本 按钮" / < asp:CustomValidator ID =" ControlToValidate =" 运行 =" ErrorMessage =" *" ClientValidationFunction =" > < /asp:CustomValidator >
/)) { args.IsValid = true tb.style.backgroundColor = "white"; tb.style.color="green"; } else { tb.style.backgroundColor = "red"; } } </script> aspx page: <asp:TextBox ID="txtInput" runat="server"></asp:TextBox> <asp:Button ID="btnSubmit" runat="server" Text="Button" /> <asp:CustomValidator ID="CustomValidator1" ControlToValidate="txtInput" runat="server" ErrorMessage="*" ClientValidationFunction="validate"> </asp:CustomValidator>