必填项检查在asp.net文本框 [英] Required fields check in asp.net textbox

查看:196
本文介绍了必填项检查在asp.net文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何能实现一个clientsidevalidation包含以下文本所需的文本框:

[名称必需]
[需要地址]

preferrably我想用ASP:如果可能的CustomValidator?只有在这两个领域都数据回传将被触发?

 < HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
<头=服务器>
    <标题>< /标题>
    <脚本SRC =脚本/ jQuery的-1.4.1.min.js类型=文/ JavaScript的>< / SCRIPT>
    <脚本类型=文/ JavaScript的>变量$ J = jQuery.noConflict();< / SCRIPT>
    <脚本类型=文/ JavaScript的>
        功能otherMessageValidator_ClientValidation(来源参数){
            args.IsValid = FALSE;            VAR纳米=附加$ J(#名称);            如果(nm.val()=与&&安培; nm.val()=!!【名必需]){
                args.IsValid = TRUE;
            }            返回args.IsValid;
        };
    < / SCRIPT>
< /头>
<身体GT;
    <表ID =form1的=服务器>
    < D​​IV>
        < ASP:文本框ID =名字=服务器的ValidationGroup =valgroup>要求名称] LT; / ASP:文本框>
        < ASP:文本框ID =地址=服务器的ValidationGroup =valgroup>需要地址]< / ASP:文本框>
    < / DIV>
    < ASP:按钮=服务器ID =but1文本=走出去的OnClick =but1_Click/>
    < ASP:的CustomValidator ID =MyCustomValidator=服务器的ValidationGroup =valgroup
        ClientValidationFunction =otherMessageValidator_ClientValidation的ErrorMessage =至少有一个文本框,需要填补。 />
    < /表及GT;
< /身体GT;
< / HTML>


解决方案

一个实现这一目标是通过使用常规的前pression从JavaScript,只验证其客户端的方式。所以在提到code那么它只会去服务器,如果所有需要的验证是满足别人,它会显示在摘要div标签屏幕上的错误消息。

以下是必需的code。

 <脚本类型=文/ JavaScript的>
 功能validateFields(){                VAR消息=;
                VAR误差= FALSE;               变种名称=的document.getElementById(<%= txtname.ClientID%GT;);
                变种地址=的document.getElementById(&下;%= txtaddress.ClientID%gt;中);                //工作所需字段验证
                如果((/\\S+/.test(name.value))== FALSE){                    消息+ =输入名称+< BR>中;
                    错误= TRUE;
                }
                //工作所需字段验证
                如果((/\\S+/.test(address.value))== FALSE){                    消息+ =输入地址+< BR>中;
                    错误= TRUE;
                }
                如果(错误){
                        的document.getElementById(的ValidationSummary)的innerHTML =消息。
                        的document.getElementById(的ValidationSummary)的style.display =块。
                        返回false;
                    }其他{
                        的document.getElementById(的ValidationSummary)的innerHTML =;
                        。的document.getElementById(的ValidationSummary)的style.display =无;
                        返回true;
                    }
                }
 < / SCRIPT>

,我们需要采取一格显示的ValidationSummary和按钮

 < D​​IV ID =的ValidationSummary的风格=显示:无;浮动:左>
                                < / DIV>
  < ASP:LinkBut​​ton的ID =lnkAddNew=服务器的OnClientClick =返回validateFields()的OnClick =lnkAddNewPatient_Click>保存< / ASP:LinkBut​​ton的>

希望这将解决您的问题。

How can I implement a clientsidevalidation required textfields which contain the following texts:

[Name required] [Address required]

Preferrably I would like to use the asp:CustomValidator if possible? Only if both fields have data the postback will be triggered?

     <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">        var $j = jQuery.noConflict();</script>
    <script type="text/javascript">


        function otherMessageValidator_ClientValidation(source, args) {
            args.IsValid = false;

            var nm = $j("#name");

            if (nm.val() != "" && nm.val() != "[Name required]") {
                args.IsValid = true;
            }

            return args.IsValid;
        };
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="name" runat="server" ValidationGroup="valgroup">[Name required]</asp:TextBox>
        <asp:TextBox ID="address" runat="server" ValidationGroup="valgroup">[Address required]</asp:TextBox>
    </div>
    <asp:Button runat="server" ID="but1" Text="go" OnClick="but1_Click" />
    <asp:CustomValidator ID="MyCustomValidator" runat="server" ValidationGroup="valgroup"
        ClientValidationFunction="otherMessageValidator_ClientValidation" ErrorMessage="At least one textbox needs to be filled in." />
    </form>
</body>
</html>

解决方案

One way to achieve it is by using regular expression from javascript and validation it client side only. So in the mentioned code it will only go to server if all the required validations are fulfilled else it will show the Error message on the screen in the Summary div tag.

Following is the required code.

 <script type="text/javascript">               
 function validateFields() {

                var message = "";
                var error = false;

               var name = document.getElementById("<%=txtname.ClientID %>");
                var address = document.getElementById("<%=txtaddress.ClientID %>");

                //work as required field validation
                if ((/\S+/.test(name.value)) == false) {

                    message += "Enter Name" + "<br>";
                    error = true;
                }
                //work as required field validation
                if ((/\S+/.test(address.value)) == false) {

                    message += "Enter Address" + "<br>";
                    error = true;
                }


                if (error) {
                        document.getElementById("validationSummary").innerHTML = message;
                        document.getElementById("validationSummary").style.display = "block";
                        return false;
                    } else {
                        document.getElementById("validationSummary").innerHTML = "";
                        document.getElementById("validationSummary").style.display = "none";
                        return true;
                    }


                }
 </script>

and we need to take one div for displaying validationsummary and the button

<div id="validationSummary" style="display: none; float: left">
                                </div>
  <asp:LinkButton ID="lnkAddNew" runat="server" OnClientClick="return validateFields()" OnClick="lnkAddNewPatient_Click" >Save</asp:LinkButton>

Hope it will solve your issue

这篇关于必填项检查在asp.net文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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