Click here to Skip to main content
15,907,905 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to allow only numbers in textbox other than number should accept, i mean should not allow to type, messege need to be displayed.

<%@ Page Title="" Language="C#" MasterPageFile="~/Expense.Master" AutoEventWireup="true" CodeBehind="Register.aspx.cs" Inherits="Expense_System.Register" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <style type="text/css">
        .auto-style1 {
            width: 39%;
        }
        .auto-style2 {
            text-align: right;
            width: 138px;
        }
        .auto-style3 {
            text-align: left;
            width: 143px;
        }
        .auto-style4 {
            text-align: right;
            width: 138px;
            height: 30px;
        }
        .auto-style5 {
            text-align: left;
            height: 30px;
            width: 143px;
        }
        .auto-style7 {
            text-align: right;
            width: 138px;
            height: 29px;
        }
        .auto-style8 {
            text-align: left;
            height: 29px;
            width: 143px;
        }
        .auto-style10 {
            text-align: right;
            width: 138px;
            height: 27px;
        }
        .auto-style11 {
            text-align: left;
            height: 27px;
            width: 143px;
        }
        .auto-style13 {
            text-align: right;
            width: 138px;
            height: 28px;
        }
        .auto-style14 {
            text-align: left;
            height: 28px;
            width: 143px;
        }
        .auto-style16 {
            text-align: left;
            height: 27px;
            width: 165px;
            margin-left: 80px;
        }
        .auto-style17 {
            text-align: left;
            width: 165px;
        }
        .auto-style18 {
            text-align: left;
            height: 29px;
            width: 165px;
        }
        .auto-style19 {
            text-align: left;
            height: 28px;
            width: 165px;
        }
        .auto-style20 {
            text-align: left;
            height: 30px;
            width: 165px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    
    <asp:Menu ID="Menu1" runat="server" BackColor="#FFFBD6" DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#990000" Orientation="Horizontal" RenderingMode="Table" StaticSubMenuIndent="10px" style="font-weight: 700; color: #FFFFFF; background-color: #99CCFF; font-size: xx-large; text-align: center;" Width="99%" Height="67px">
                <DynamicHoverStyle BackColor="#990000" ForeColor="White" />
                <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
                <DynamicMenuStyle BackColor="#FFFBD6" />
                <DynamicSelectedStyle BackColor="#FFCC66" />
                <Items>
                 <%--   <asp:MenuItem NavigateUrl="~/HomePage.aspx" Text="Home Page" Value="Home Page"></asp:MenuItem>--%>
                    <asp:MenuItem NavigateUrl="~/Loginpage.aspx" Text="Login" Value="Login"></asp:MenuItem>
                </Items>
                <StaticHoverStyle BackColor="#990000" ForeColor="White" />
                <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
                <StaticSelectedStyle BackColor="#FFCC66" />
            </asp:Menu>
  

    <h1> Fill the below details to register</h1>

    <table class="auto-style1" border="1">
        <tr>
            <td class="auto-style10">Full Name:</td>
           
            <td class="auto-style16">
                <asp:TextBox ID="tbfullname" runat="server" Width="160px" ></asp:TextBox>     
            </td>

            <td class="auto-style11">
                <asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" controltovalidate="tbfullname" ErrorMessage="Name is mandatory" ForeColor="Red"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="auto-style2">Email id:</td>
            <td class="auto-style17">
                <asp:TextBox ID="tbemail" runat="server" TextMode="Email" Width="160px"></asp:TextBox>
            </td>
            
            <td class="auto-style3">
                <asp:RequiredFieldValidator ID="RequiredFieldValidatormail" runat="server" ControlToValidate="tbemail" ErrorMessage="Email Required" ForeColor="Red"></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="RegularExpressionValidatormail" runat="server" ControlToValidate="tbemail" ErrorMessage="invalid email" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ForeColor="Red"></asp:RegularExpressionValidator>
            </td>
        </tr>
        <tr>
            <td class="auto-style7">Phone Number:</td>
            <td class="auto-style18">
                <asp:TextBox ID="tbphoneno" runat="server" TextMode="Phone" Width="160px" OnTextChanged="tbphoneno_TextChanged" ></asp:TextBox> 
            </td>
            <td class="auto-style8">
                <asp:RegularExpressionValidator ID="RegularExpressionValidatorphoneno" runat="server" ControlToValidate="tbphoneno"  ErrorMessage="Phone number is invalid" ValidationExpression="[0-9]{10}" ForeColor="Red"></asp:RegularExpressionValidator>
                <asp:RequiredFieldValidator ID="RequiredFieldValidatorphoneno" runat="server" ControlToValidate="tbphoneno" ErrorMessage="Phone number is Required" ForeColor="Red"></asp:RequiredFieldValidator>
                
            </td>
        </tr>
        <tr>
            <td class="auto-style13">Date of birth:</td>
            <td class="auto-style19">
                <asp:TextBox ID="tbdob" runat="server" TextMode="Date" Width="160px"></asp:TextBox>
            </td>
            <td class="auto-style14">
                <asp:RequiredFieldValidator ID="RequiredFieldValidatordob" runat="server" ControlToValidate="tbdob" ErrorMessage="Date of birth is Required" ForeColor="Red"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="auto-style2">Login id:</td>
            <td class="auto-style17">
                <asp:TextBox ID="tbloginid" runat="server" Width="160px"></asp:TextBox>
            </td>
            <td class="auto-style3">
                <asp:RequiredFieldValidator ID="RequiredFieldValidatorloginid" runat="server" ControlToValidate="tbloginid" ErrorMessage="Login id is Required" ForeColor="Red"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="auto-style4">Password:</td>
            <td class="auto-style20">
                <asp:TextBox ID="tbpwd" runat="server" TextMode="Password" Width="160px"></asp:TextBox>
            </td>
            <td class="auto-style5">
                <asp:RequiredFieldValidator ID="RequiredFieldValidatorpwd" runat="server" ControlToValidate="tbpwd" ErrorMessage="password is Required" ForeColor="Red"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="auto-style4">Confirm Password:</td>
            <td class="auto-style20">
                <asp:TextBox ID="tbconfirmpwd" runat="server" TextMode="Password" Width="160px"></asp:TextBox>
            </td>
            <td class="auto-style5">
                <asp:RequiredFieldValidator ID="RequiredFieldValidatorconpwd" runat="server" ControlToValidate="tbpwd" ErrorMessage="Password is Required" ForeColor="Red"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="auto-style4"> </td>
            <td class="auto-style20">
                <asp:Button ID="btnRegsubmit" runat="server" Text="SignUp" OnClick="btnRegsubmit_Click" />
                <input id="RegReset" type="reset" value="Reset" /></td>
            <td class="auto-style5">
                 </td>
        </tr>
    </table>

  

    </asp:Content>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Text.RegularExpressions;


namespace Expense_System
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
         

        }

        protected void btnRegsubmit_Click(object sender, EventArgs e)
        {
        
            String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;

            SqlConnection con = new SqlConnection(strConnString);

            con.Open();

            SqlCommand cmd = new SqlCommand();

            cmd.CommandType = CommandType.StoredProcedure;

            cmd.CommandText = "[dbo].[SpCreateUser]";

            cmd.Parameters.Add("@Full_name", SqlDbType.VarChar).Value = tbfullname.Text;
            cmd.Parameters.Add("@Dob", SqlDbType.Date).Value = tbdob.Text;
            cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = tbemail.Text;
            cmd.Parameters.Add("@phoneno", SqlDbType.Int).Value = tbphoneno.Text;
            cmd.Parameters.Add("@loginid", SqlDbType.VarChar).Value = tbloginid.Text;
            cmd.Parameters.Add("@pwd", SqlDbType.VarChar).Value = tbpwd.Text;

            cmd.Connection = con;

            int totalrecordscreated =(int)cmd.ExecuteNonQuery();
            Response.Write("No of created records " + totalrecordscreated.ToString());


            con.Close();
    
           
      

        }

        protected void tbphoneno_TextChanged(object sender, EventArgs e)
        {
            if (!Regex.IsMatch(tbphoneno.Text,
  @"(^([0-9]*|\d*\d{1}?\d*)$)"))
            {
                
            }
            else
            {

            }
        }

      

      
    }
}


What I have tried:

 protected void tbphoneno_TextChanged(object sender, EventArgs e)
      {
          if (!Regex.IsMatch(tbphoneno.Text,
@"(^([0-9]*|\d*\d{1}?\d*)$)"))
          {

          }
          else
          {

          }


Is this correct? but not validating.
Posted
Updated 6-Aug-18 21:16pm
Comments
Richard Deeming 7-Aug-18 13:47pm    
NEVER store passwords in plain text!

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]


And why are you re-inventing the wheel? ASP.NET has several perfectly good authentication systems built-in - for example, ASP.NET Identity[^]

1 solution

Basically, don't do it in C# - that only runs at the Server, and that means each time the user presses a key, you need a post back to the server to to the validation. That's slow and inefficient - do it in the Client where the validation is all local: javascript numeric only input field - Google Search[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900