You Can use it by two ways:-
1. Javascript
just add this script on page's HTML side
<script type="text/javascript">
function checking()
{
var check1 = document.getElementById("<%=yourtextboxid.ClientID %>").length;
if(var<>4)
{
alert('Please Enter Only 4 digit no.');
return false;
}
else
{
return true;
}
}
function CheckNemericValue()
{
if((event.keyCode >= 48 && event.keyCode <= 57) )
{
event.keyCode = event.keyCode;
}
else
{
alert("Only Numbers are allowed");
event.keyCode = 0;
return false
}
}
</script>
and on page load event
protected void Page_Load(object sender, EventArgs e)
{
yourtextboxid.Attributes.Add("onkeypress", "return CheckNemericValue(event);");
}
and on Button just add this javascript function like this.
<asp:button id="Button1" runat="server" text="Add" onclientclick="return checking()" onclick="Button1_Click" xmlns:asp="#unknown" />
this will help you.
and the
2. By validation controls
use
<asp:regularexpressionvalidator id="RegularExpressionValidator1" runat="server" errormessage="Please Enter numeric Value Which contains 4 Digits Only" controltovalidate="yourtextboxid" setfocusonerror="True" validationexpression="^([0-9]{4})$" validationgroup="validation" xmlns:asp="#unknown">
You can also set text box's maximum length=4. so the user can't input more than 4 digit.//this is optional
tell me if for any error.
Mark it as Answer if it help you.