Friday, 27 February 2015

Allow Only Number To TextBox Avoid Character Using RegularExpression Validation in Asp.Net C#

No comments    
categories: , , ,
Allow Only Number  To TextBox  Avoid Character 


Textbox Enter Input Only Allowed Number  But Not Allow Character   RegularExpression Validation Using Asp.Net C#.

DEMO



HTML CODING


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
        <asp:TextBox ID="txtNumber" runat="server" Height="16px" AutoPostBack="True" ForeColor="#CC3300" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
    </form>
</body>
</html>



                           C# CODING


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

    }  
  
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {


  if (System.Text.RegularExpressions.Regex.IsMatch(txtNumber.Text, "[^0-9]"))
        {

        Response.Write("<script>alert('Only Allowed Numbers')</script>");

            int count = txtNumber.Text.Count();

    txtNumber.Text = txtNumber.Text.Remove(txtNumber.Text.Length - count);

        }
        else
        {

            Response.Write("<script>alert('Valid Numbers')</script>");

        }
    }
}




















0 comments:

Post a Comment