Stopping user input of numeric value in texbox when using c #

I am trying to prevent the user from entering a numeric value in a search criteria textbox when the dropdown value is MBID. The code works. I need help troubleshooting a validation issue.

enter image description here

C # code

protected bool searchData()
{
      string val = DropDownList1.SelectedItem.Value;
 switch (val)
  {
      case "MBID":

       using (SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["Molecular"].ConnectionString))
         {
             con.Open();
               using (SqlCommand cmd = new SqlCommand(@"SELECT  count(*)

                        from Patient  where MBID = @SearchCriteria ", con))
                {

                        DataTable dt = new DataTable();
                        SqlDataAdapter da = new SqlDataAdapter(cmd);
                        int userCount = (Int32)cmd.ExecuteScalar();
                        da.Fill(dt);
           }
       }

    }

  }

      

The "Search" button calls the code below

protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (FormValidation()) if (searchData())
         {

            BindGrid();
            inputUpdateFornData();

        }

    }

      

HTML code

<asp:DropDownList ID="DropDownList1" runat="server"  OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
      <asp:ListItem Value="MBID">MBID</asp:ListItem>
      <asp:ListItem Value="HPNumber">HP Number</asp:ListItem>
      <asp:ListItem Value="NHSNumber">NHS Number</asp:ListItem>

      

I cannot use the code below because a numeric value could be entered for another dropdown menu option

<asp:TextBox ID="txtSearchCriteria" runat="server />
<asp:CompareValidator ID="cv" runat="server"     ControlToValidate="txtSearchCriteria" Type="Integer"
  Operator="DataTypeCheck" ErrorMessage="Value must be an integer!" />

      

+3


source to share


3 answers


hi please try below code to validate javascript below - javascript code

 <script>
        function validateSearch()
        {
            var dropdown = document.getElementById("<%= ddlSearch.ClientID %>");
            var txtSearch = document.getElementById("<%= txtSearch.ClientID %>");
            if (dropdown.value == "MBID")
            {
                var reg = /^\d+$/;
                if (!reg.test(txtSearch.value)) {
                    alert("Please enter proper value");
                    txtSearch.focus();
                    return false;
                }
            }
        }

            </script>

      



and below is the aspx code of the dropdown menu and the textbox.

 <asp:DropDownList runat="server" ID="ddlSearch">
            <asp:ListItem Text="Select" Value="-1"></asp:ListItem>
            <asp:ListItem Text="MBID" Value="MBID"></asp:ListItem>
            <asp:ListItem Text="Name" Value="Name"></asp:ListItem>
        </asp:DropDownList>
        <asp:TextBox runat="server" ID="txtSearch"></asp:TextBox>
        <asp:Button runat="server" ID="btnSearch" Text="Search" OnClientClick="return validateSearch();" />

      

0


source


Here's how to do it:



        try
        {
            int d = int.Parse(txtSearchCriteria.Text);
            //if works str is a number
        }
        catch (Exception e)
        {
            Console.WriteLine("It isn't a number!");
        }

      

0


source


Add ClientValidationFunction="validateFunction"

to your CompareValidator.

And write the check function as follows -

function validateFunction(s,args){
   if (yourdropdown.value == "MBID")
   {
        // Your code
   }
}

      

If you want to test the server, add OnServerValidate

an attribute and a server side handler function.

0


source







All Articles