验证文本框是否仅包含数字 [英] Validating whether a textbox contains only numbers

查看:42
本文介绍了验证文本框是否仅包含数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个主意,因为我发现很难为txtbox编写代码,该代码只允许使用c#在mysql中不允许整数,而不允许使用字母.我的计划是为什么不将数据库列设置为整数而不是典型的varchar,并且如果您放了一个字母,它当然会变成异常,因此在这种情况下,我想捕获异常并提示一个消息框,提示请仅输入整数".你觉得呢?

So I have an idea, because i find it hard to make a code for txtbox that will only allow integer and not a letters in mysql using c#. My Plan is why not set the database column into integer instead of typical varchar and if ever you put a letter of course it will turn and exception so in that case I want to catch the exception and prompt a messagebox saying "Please enter only integer". What do you think?

推荐答案

对要存储在其中的数据使用正确的列数据类型是个好主意,但是很容易检查字符串是否仅包含数字-仅解析并查看是否返回错误:

It's a good idea to use the correct column datatype for what you plan to store in it, but it's very easy to check whether a string contains only numbers - just parse and see if an 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

这篇关于验证文本框是否仅包含数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆