【问题标题】:Range Validation in asp.net [closed]asp.net中的范围验证[关闭]
【发布时间】:2014-07-03 14:41:38
【问题描述】:

" 我正在 Visual Studio 上制作一个 Web 应用程序,我想主要使用 c# 代码,所以请如果有人能告诉我如何在单击提交时同时进行“必填字段”和“特定范围”验证在 asp.net 中使用 C# 编码的按钮。”

  <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

    <style type="text/css">
                body{margin:19px 10px 20px 20px }
        .div1
        {
            background-color: lightGray;
            width:47%;
            height:1615px;
            float:left;
        }

        .div2
        {
            background-color: light gray;
            width:48%;
            height:1614px;
            float: left;
            margin-left: 5px;
        }
 .div3
        {
            background-color: black;
            width:2PX;
            height:1615px;
            float: left;

     }

        .container
        {
            width:100%;
            height: 1620px;
        }

        #TextArea1
    {
        height: 73px;
        margin-top: 0px;
            width: 244px;
        }
    #Submit1
    {
        width: 163px;
    }
        #Text6
        {
            width: 36px;
            margin-left: 9px;
        }
    .footer_leftNav{float: left;
            width: 1985px;
            height: 25px;
            margin-top: 0px;
        }
        #footer .footer_nav{font-size:11px;color:#a9b4c8;margin:13px auto 0px auto;width:860px;padding:0px 0 30px 0;text-align:left;border-top:solid 0px #546589;font-weight:100;}
.footer_nav_text{float:right;text-align:right;
            width: 304px;
            height: 18px;
            margin-left: 50px;
        }
        .listyle
    { 
        height: 17px;
        width: 494px;
    }
        #Text5
        {
            width: 142px;
        }
        .style3
        {
            font-size: x-large;
            color: #000000;
        }
        .style4
    {
        font-weight: bold;
        text-decoration: underline;
        color: #660033;
    }
    .style5
    {
        color: #660033;
    }
    .style6
    {
        color: #660033;
        font-family: "Bookman Old Style";
    }
    .style7
    {
        font-weight: 700;
        background-color: #FFCCCC;
    }
    .style8
    {
        color: #660033;
        background-color: #FFCCCC;
    }
    .style9
    {
        font-size: x-large;
    }
    #Text11
    {
        height: 25px;
        width: 155px;
        margin-top: 0px;
            margin-left: 0px;
        }
    .style11
    {
        font-size: small;
        color: #000000;
    }
    .style12
    {
        text-decoration: underline;
    }
    #Submit2
    {
        height: 30px;
        width: 113px;
    }
        </style>
</asp:Content>

<asp:Content ID="Content2" runat="server" ContentPlaceHolderID="MainContent">
    <h1>strong><span class="style3">&nbsp;&
        </span><span class="style9">
        <span class="style4">&nbsp;PROCUREMENT REQUEST AND TRACKING&nbsp;</span></span><span class="style3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></strong></h1>
<h1><strong><span class="style3"> 


    <h1><strong><span class="style3"> 

        </span></strong>
    </h1>


    <div class="container">
        <div class="div1"><h1> 
            <span class="style6"><strong><span class="style7">MAKE A REQUEST !!</span></strong></span></h1>

<p>&nbsp;EMAIL&nbsp;ID*:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>&nbsp;&nbsp;&nbsp;&nbsp;</p>
            <    </p>
 zzz           
<p><span class="smallred">*</span>
EXTENSION:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox3" MaximumValue="4" MinimumValue="4" Type="Integer" Text="Invalid Extension Number" EnableClientScript="false" ErrorMessage="RangeValidator"></asp:RangeValidator>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mobile NUMBER:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>

     </p>
<p><span class="smallred">*</span>
DEPARTMENT:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input id="Text4" type="text" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PROJECT
  <input type="radio" name="portal" id="radioprojectOff" onclick="inputname(this);"/> &nbsp; 
    *<input type="text" name="textprojectOff" id="TextProjectOff" value="Project Name" onclick="inputname(this);"/> 
    NON-PROJECT<input type="radio" name="portal"id="radiononprojectoff" /> &nbsp; 

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

        var fieldId = inField.id;
        var type = fieldId.substring(0, 4);

        if (type == 'text') {
            var name = fieldId.substring(4);
            var radioButton = document.getElementById("radio" + name);
            radioButton.checked = true;
        } else {
            var name;
            var textField;
            name = fieldId.substring(4);
            textField = document.getElementById("TextProjectOff");
//            textField = document.getElementById("text" + name);
            textField.focus();
        }
    }
</script>


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

        if (document.getElementById("TextBox4").value == "") {
            alert("*mandatory fields");
            document.getElementById("TextBox4").focus();
            return false;
        }
        if (document.getElementById("TextBox3").value == "") {
            alert("*mandatory fields");
            document.getElementById("TextBox3").focus();
            return false;
        }
         if (document.getElementById("Text4").value == "") {
            alert("*mandatory fields");
            document.getElementById("Text4").focus();
            return false;
            }
            if (document.getElementById("TextProjectOff").value == "") {
                alert("*mandatory fields");
                document.getElementById("textprojectoff").focus();
                return false;
            }
            if (document.getElementById("Text5").value == "") {
                alert("*mandatory fields");
                document.getElementById("Text5").focus();
                return false;
            }
            if (document.getElementById("Text6").value == "") {
                alert("*mandatory fields");
                document.getElementById("Text6").focus();
                return false;
            }
            if (document.getElementById("Text7").value == "") {
                alert("*mandaory fields");
                document.getElementById("Text7").focus();
                return false;
            }
        return true;
    }
</script>






&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<p>&nbsp;<p>*&nbsp;PRODUCT NAME:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input id="Text5" type="text" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <span class="smallred">*</span>
QUANTITY&nbsp; : <input id="Text6" type="text" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; COST CENTER:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input id="Text9" type="text" /></p>
            <p>&nbsp;</p>
<p>REQUIREMENT LEVEL:&nbsp;&nbsp;
    <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem>NORMAL</asp:ListItem>
        <asp:ListItem>URGENT</asp:ListItem>
        <asp:ListItem>WITHIN A MONTH</asp:ListItem>
    </asp:DropDownList>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    <span class="smallred">*</span>
TARGET DATE FOR PROCUREMENT: <input id="Text7" type="text" /></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
    <p>
    <textarea id="TextArea1" name="S1"></textarea></p>

<asp:Button ID="btnSubmit1" OnClientClick=" return validate()" runat="server" 
            Text="Submit" Height="34px" Width="81px" />


            </p>

    </p>

div>
<div class="div3"> </div>

<div class="div2"><h1> 

      <script language="javascript" type="text/javascript">

          function valid() {
              if (document.getElementById("Text10").value == "") {
                  alert("#mandatory fields");
                  document.getElementById("Text10").focus();
                  return false;
              }
              if (document.getElementById("Text11").value == "") {
                  alert("#mandatory fields");
                  document.getElementById("Text11").focus();
                  return false;
              }
              return true;
          }

        </script>  <span class="style8"><strong style="font-weight: 700">TRACK PRN!!</strong></span></h1>
            <p>

                #EMAIL ID:&nbsp;&nbsp;&nbsp;
        <input id="Text10" type="text" />
    </p>
<p>

        <strong><span class="style5">&nbsp;<span class="style9">#ENTER PRN:</span></span></strong></p>
    <p>

        <strong><span class="style5"><span class="style9">



            <input id="Text11" type="text" />


    <p class="style11">

        <span class="style12">&nbsp;&nbsp;Enter A Valid PRN 
        (Procurement Request Number)</span></p>
    <p class="style11">
        &nbsp;</p>
<p>



     <asp:Button ID="Button1" OnClientClick=" return valid()" runat="server" 
            Text="Submit" Height="34px" Width="81px" />
        </div>

    </div>
   </asp:Content>

我不想使用如此冗长的编码,并且在运行程序时遇到了一些运行时错误。请有人修改它并帮助我。

这是我的实习项目,所以请尽快帮助我。

【问题讨论】:

  • 嘿 @Shruti 看着你的分数,我猜你是 stackoverflow 的新手吧!请以适当的方式解释,以便我们可以轻松调查您的问题..!使用服务器端验证或客户端(javascript)?您需要验证哪个字段?
  • 至少告诉我您需要验证哪个控件?不要发布整页代码..!只是你现在卡住的几行代码..?
  • 是的..m 在这里和 asp.net 中都是新的..
  • 我想对我的所有文本框进行验证,并对其中一些文本框进行范围验证,例如扩展名、手机号码……对它们进行数据类型控制……这些问题可以通过 RangeValidator 解决……我的问题是我希望当用户单击提交按钮时应该检查这些验证...
  • 是的,我需要服务器端验证

标签: c# javascript asp.net validation


【解决方案1】:

对于将字段设为MANDATORY

使用RequiredFieldValidator:

<asp:TextBox ID="txtName" runat="server"></asp:TextBox> 

    <asp:RequiredFieldValidator  
         ID="RequiredFieldValidator1"  
         runat="server"  
         ControlToValidate="txtName"  
         ErrorMessage='Input Country!'  
         EnableClientScript="true"  
         SetFocusOnError="true"  
         Text="*"> 
</asp:RequiredFieldValidator>

要验证TextBox 中的数字范围,请使用RangeValidator

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox1" MaximumValue="200" MinimumValue="100" Type="Integer" ErrorMessage="Please input between 100 to 200.">  
</asp:RangeValidator>  

Read more about Validations Here

希望对您有所帮助!

【讨论】:

  • -thnkeww 先生......这是一个很大的帮助......现在我只在那些我给出相同最大值和最小值的文本框中得到一个新错误(它是必需的)请告诉我如何解决它.. EXTENSION* : &lt;asp:TextBox ID="textBox1" runat="server" CausesValidation="true" CssClass="txtbox"&gt;&lt;/asp:TextBox&gt; &lt;asp:RangeValidator ID="rangeValidator1" runat="server" ControlToValidate="textBox1" Type="integer" MaximumValue="4" MinimumValue="4" ValidationGroup="valid" ForeColor="Red" ErrorMessage="invalid extension" /&gt;
  • 如果您不想要 TextBox 的范围验证器,而只想将其设为必填字段,则删除 rangeValidator 控件并添加所需的字段验证器..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 2014-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多