سؤال

" I m making a web application on visual studio and i want to use c# codes mostly so please if any one can tell me how to make both "mandatory field" as well as" specific range" validation on the click of submit button using C# coding in asp.net."

  <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>

I don't want to use such a lengthy coding and there are some runtime error which I am encountering during running the program..plz someone modify it and help me .

its my Internship project so try n help me as soon as possible.

هل كانت مفيدة؟

المحلول

For making field MANDATORY:

Use 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>

For Validating Range of Number in TextBox use 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

Hope this helps you!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top