Validating a text field that allows only numeric values

Possible duplicate:
How to make a text box that accepts only numbers?

I have a phone number that I want to save as a string.

I read this when using

txtHomePhone.Text 

What seems necessary to me is something like a number, but cannot make it work.

 if (txtHomePhone.Text == //something.IsNumeric) { //Display error } else { //Carry on with the rest of program ie. Add Phone number to program. } 

What is the best way to enter only numeric values?

+4
source share
6 answers

Since txtHomePhone represents a TextBox , you can use the KeyPress event to accept the characters you want to allow and reject what you would not want to allow in txtHomePhone

Example

 public Form1() { InitializeComponent(); txtHomePhone.KeyPress += new KeyPressEventHandler(txtHomePhone_KeyPress); } private void txtHomePhone_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '') //The character represents a backspace { e.Handled = false; //Do not reject the input } else { e.Handled = true; //Reject the input } } 

Note : The following character (which is not displayed) represents the inverse space.
Note You can always enable or disable a specific character using e.Handled .
Note You can create a conditional statement if you want to use - , , ( or ) only once. I would recommend that you use regular expressions if you want these characters to be entered at a specific position.

Example

 if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '') //The character represents a backspace { e.Handled = false; //Do not reject the input } else { if (e.KeyChar == ')' && !txtHomePhone.Text.Contains(")")) { e.Handled = false; //Do not reject the input } else if (e.KeyChar == '(' && !txtHomePhone.Text.Contains("(")) { e.Handled = false; //Do not reject the input } else if (e.KeyChar == '-' && !textBox1.Text.Contains("-")) { e.Handled = false; //Do not reject the input } else if (e.KeyChar == ' ' && !txtHomePhone.Text.Contains(" ")) { e.Handled = false; //Do not reject the input } else { e.Handled = true; } } 

Thanks,
Hope you find this helpful :)

+8
source

I assume you are using Windows Forms here, look at MaskedTextBox . It allows you to specify a character input mask.

 txtHomePhone.Mask = "##### ### ###"; 

Since this allows you to limit the input values, you can safely parse the value to an integer.

Note. If you use WPF, I don’t think MaskedTextBox is in the base libraries, but there are extensions available on NuGet that can provide similar functionality.

+7
source

try it

 if (!txtHomePhone.Text.All(c=> Char.IsNumber(c))) { //Display error } else { //Carry on with the rest of program ie. Add Phone number to program. } 
+3
source

To check if a numerical value has been entered, you can use Integer.TryParse

 int num; bool isNum = Integer.TryParse(txtHomePhone.Text.Trim(), out num); if (!isNum) //Display error else //Carry on with the rest of program ie. Add Phone number to program. 

BUT, remember that phone numbers are not necessarily only numeric. Pay attention to Trevor Pille's answer for a hidden text box.

+3
source

Normally, I would use Integer.TryParse as the recommended davenewza, but another alternative is to use the VisualBasic IsNumeric function from C #.

Add a link to the Microsoft.VisualBasic.dll file, and then use the following code.

 if (Microsoft.VisualBasic.Information.IsNumeric(txtHomePhone.Text)) { //Display error } else { //Carry on with the rest of program ie. Add Phone number to program. } 
0
source

The best way I've found this is to allow the user to enter numeric keys in the text box, thanks for your help.

0
source

Source: https://habr.com/ru/post/1444381/


All Articles