Checking for only numbers in the text box

So, I have an idea, because it's hard for me to make code for txtbox that will only use numbers, not letters in mysql, using C #. For some reason, my plan does not set the database column to an integer instead of the typical varchar, and if you ever post a letter, it will of course turn into an exception, so in this case I want to catch the exception and cause a message with the message "Please enter only an integer, "What do you think?

+4
source share
3 answers

Itโ€™s a good idea to use the correct column type for the column for what you plan to store in it, but itโ€™s very easy to check if the row contains only numbers - just analyze and see if the error is returned:

int parsedValue; if (!int.TryParse(textBox.Text, out parsedValue)) { MessageBox.Show("This is a number only field"); return; } // Save parsedValue into the database 
+14
source

You can achieve it this way.

  int outParse; // Check if the point entered is numeric or not if (Int32.TryParse(propertyPriceTextBox.Text, out outParse) && outParse) { // Do what you want to do if numeric } else { // Do what you want to do if not numeric } 
0
source

This is a very simple task ... You can easily ensure that the user enters a number field. Visual Studio has built support for this (and you can also achieve this through coding). Here's what you need to do: - Switch to the design view from the layout view. Now click on the projector and press Ctrl + Alt + X. From the toolbar that opens, click Verify and drag the comparison checker next to your text box. Go back to the comparison validator and select properties, now find ErrorMessage and write "Alert: Type only Number". In the control to check, select your control. In the Statement, select DataTypeCheck and in the Type field select Integer. That's all. Also with the help of encoding you can get it as: -

  protected void Button1_Click(object sender, EventArgs e) { int i; if (!int.TryParse(textBox.Text, out i)) { Label.Text ="This is a number only field"; return; } 
0
source

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


All Articles