473,396 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

textbox validation

i am developing a application .net2.0 c# windows application,
when i press the enter key, it should go to the next textbox, if other than numbers is pressed in first textbox, message box should tell "pls enter a numeric value only" so it should accept only numbers and not others
Sep 26 '08 #1
2 1271
Curtis Rutland
3,256 Expert 2GB
i am developing a application .net2.0 c# windows application,
when i press the enter key, it should go to the next textbox, if other than numbers is pressed in first textbox, message box should tell "pls enter a numeric value only" so it should accept only numbers and not others
To start, this isn't the proper way to ask questions. Actually, you never asked a question, you just told us your requirements. Nobody will do your program for you.

MODERATOR

However, I can help you get started.

As for moving to the next fields with the enter key, you will need to add an event handler to the textboxes' KeyDown event. Then check if the key was enter. The KeyEventArgs e should hold this data. Make a separate listener for each textbox, and on the case that enter is pressed, .Focus() on the next control.

As for the numeric text box, you have a couple of options. You can replace your TextBox with a NumericUpDown control. That will take numeric input as well as giving you buttons to click up and down. You can use a MaskedTextBox, and apply a mask that will only accept numbers. You can have running validation in the textbox's TextChanged event. Or, you can do your validation in the same event where you check for the Enter key.
Sep 26 '08 #2
shweta123
692 Expert 512MB
Hi,

You need to check for following 2 things in your C# code for your problem :

1> Check if enter key is pressed or not in the first textbox and write the necessary code if key pressed is Enter key. For this you have to write the following code in keypress event of that textbox.

e.g.
Expand|Select|Wrap|Line Numbers
  1.  
  2.       private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  3.         {
  4.             int c= Convert.ToInt32(e.KeyChar);
  5.  
  6.             //check if enter key is pressed
  7.             if (c == 13)
  8.             {
  9.               //set focus on the next textbox
  10.                 textBox2.Focus();
  11.             }
  12.         }
  13.  
2> Check if text inputted into the textbox contains only numbers or not.
You can try following function for doing that.

Expand|Select|Wrap|Line Numbers
  1. // function for checking if numeric value is input into the textbox
  2. bool MatchRegex(string input)
  3. {
  4.     // Regex match
  5.     RegexOptions   options = RegexOptions.None;
  6.     Regex regex = new Regex(@"^[0-9]*$", options);
  7.  
  8.  
  9.     // Check for match
  10.     bool   isMatch = regex.IsMatch(input.ToString());
  11.  
  12.     if (isMatch)
  13.     {
  14.         //if value in the textbox is numeric
  15.         return true;
  16.     }
  17.     else
  18.     {
  19.         //if value in the textbox is not numeric
  20.         return false;
  21.     }
  22. }
  23.  

i am developing a application .net2.0 c# windows application,
when i press the enter key, it should go to the next textbox, if other than numbers is pressed in first textbox, message box should tell "pls enter a numeric value only" so it should accept only numbers and not others
Sep 26 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Pham Nguyen | last post by:
Has anyone seen an example of a textbox server control that has built-in client-side validation? I'd like to build a server control that extends the System.Web.UI.WebControls.TextBox class to allow...
1
by: Kum | last post by:
Hi, I need help in asp.net dynamic textbox controls validation. I am creating textbox controls dynamically on a asp.net webpage. Now after creating the textboxes on the page I want to validate...
2
by: Oleg Ogurok | last post by:
Hi there, I have a modified ASP.NET TextBox control that formats its output as the phone number, e.g. if you enter "1234567890" it'll change the value to "123-456-7890". This is done by a...
14
by: teddysnips | last post by:
WINDOWS FORMS I've a form that has a textbox that allows the user to enter a string. On the LostFocus event, the textbox formats the string into a preferred format. However, if the user...
1
by: bsm | last post by:
I have created one User controls which contains 1. Textbox and 2. ImageButton (to display calendar) (I tried to create my own datetime control). I have created "onblur" event on textbox event,...
0
by: Mike | last post by:
So here's the situation (.NET 2.0 btw): I have a form, and on this form is a textbox among many other databound controls. The textbox is bound to a field in a data table via the Text property. ...
2
by: Christian Cambier | last post by:
Hello, I have a textbox in which I only want to display numbers when the focus is set on the textbox and the user presses a key on the kb. Easy with html-control <input id="htmlTxtVal2"...
9
mageswar005
by: mageswar005 | last post by:
Hi Guys, My array textbox validation is not working fine,my code is given below please help me immediately. function validation() { var chks =...
2
by: mrutyunjaya | last post by:
hello, i have two textbox column and one button control in gridvie when i click button it will ask plese enter price in first textbox . when enter values it is asking again. how to pass...
4
by: HenrikL | last post by:
Hi. I have a textbox that have a binding on it with converter and validationrules. When a validation error ouccur the foreground of the textbox will change to red cause it has a style.triggers...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.