Click here to Skip to main content
15,901,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Day,

I am creating a web form and I am trying to validate some textboxes e.g. I don't want the end user to insert letters in ID text box..

Here are my codes below and please advise me if I am using an old method


C#
protected void txtID_TextChanged(object sender, EventArgs e)
       {
           foreach (char cr in txtID.Text)
           {
               if (char.IsDigit(cr) == false)
               {
                   Console.Write("Invalid Charactor"","Error Message")         

               }

           }


I have double click the text box and started my coding.....
Posted
Updated 5-Jun-13 23:13pm
v4
Comments
Jameel VM 6-Jun-13 5:12am    
what validation you need?only number?
Nkhanedzeni 6-Jun-13 5:16am    
only numbers on ID textbox

Hello,

You may use a custom validator control.
Have a look on this : CustomValidator, contrôle - MSDN - Microsoft
 
Share this answer
 
v2
Comments
Nkhanedzeni 6-Jun-13 5:44am    
Thanks....do you have any idea on what will be the problem with my coding?
Ahmed Bensaid 6-Jun-13 6:06am    
You're welcome.
You need to initialize the AutoPostBack parameter of the TextBox to True. And press enter when you finish writing in this TextBox. To generate a PostBack to the server.
Can you explain me what do you want to do accurately ?
Cause, if you want some live validation when the user is writing in the TextBox, you'll need some client code (JavaScript).
And if your application is an ASP.NET application, why are you using Console.Write() ?
Nkhanedzeni 6-Jun-13 6:21am    
It is just for learning purpose since i knew by coding
Ahmed Bensaid 6-Jun-13 6:27am    
Ok, so you should use the MSDN examples of the tutorial I gave you ;)
Nkhanedzeni 6-Jun-13 6:29am    
so i must not use Console.Write()?
Try this
VB
<asp:CompareValidator runat="server" Operator="DataTypeCheck" Type="Integer"
 ControlToValidate="ValueTextBox" ErrorMessage="Value must be a whole number" />

Hope this helps
 
Share this answer
 
Comments
Nkhanedzeni 6-Jun-13 6:02am    
Thanks let me try this....i will get back to you soon
Nkhanedzeni 6-Jun-13 6:16am    
thanks......working
Jameel VM 6-Jun-13 6:32am    
Please mark it as a 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