ASP.Net验证控件和Javascript [英] ASP.Net Validation Controls and Javascript

查看:74
本文介绍了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>


这篇关于ASP.Net验证控件和Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆