Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Dear Friends,

am working on asp.net c#, Sqlserver 2005.

I have a textbox on my webpage...How to Set the validation for that Textbox.

I need to enter only numbers in that textbox....it should not take alphabets.

If the user enters alphabets in that textbox...it must show a message. Please enter only numbers.

Please show me,

Thanks.
Posted
Updated 15-Nov-16 23:00pm

 
Share this answer
 
Try this:
C#
void NumberValidation(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
 
Share this answer
 
Comments
Snehalata_Patil 15-Mar-13 6:44am    
it works on my project.
but when i add it into database it does not work.
what to do???
Prasad_Kulkarni 15-Mar-13 7:10am    
What does that means? Add into database means? You're getting only numeric values wherever you're trying to use this method, and just simply you need to pass it with query to database..
Snehalata_Patil 15-Mar-13 7:33am    
sir, actually validations works on form.
but that particular entry doesn't save into database.
pls reply..
Prasad_Kulkarni 15-Mar-13 11:41am    
Doesn't save means?? Is it throwing any error or what?

One more thing;
check your query [how you're sending this field to database],
check you database which is the type you have assigned to this field.
Probably you could achieve it using Jquery[^] too.
 
Share this answer
 
For this you can also use FilterTextboxExtender of Ajax. You can also set only for alphabets or both.
 
Share this answer
 
This small javascript file contains already what you need. Get it from here and also find how to use it in the article text.

A Tiny Javascript Framework for Common Validation Scenarios.[^]

This contains all such common methods which are required by developers very often. I hope you will also find it useful.
 
Share this answer
 
can you try this code

XML
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Numeric Only" ControlToValidate="TextBox1" ValidationExpression="(^[1-9]\d*$)"></asp:RegularExpressionValidator>

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Required" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
 
Share this answer
 
Use reqular expression to validate the control input during the KeyDown event
C#
btn_keyDown(object e , sender eventArgs)
{
    if(Regex.Match(txtBox1.Text, "[0-9]\d*$"))
    {
       ..............
    }
}
 
Share this answer
 
@Ranjith Reddy CSE use this javascript code with keyup event.........It will definitely help you........


C#
function phone() {
             code = "<%=txtphonenumber.ClientID %>";

             if (!document.getElementById(code).value.match(/^[0-9]+$/) && document.getElementById(code).value != "") {
                 document.getElementById(code).value = "";
                 document.getElementById(code).focus();
                 alert("Please enter phone no. in digits");
                 return false;
             }
         }



all d best...
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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