如何仅验证数据表列中的数字? [英] How to validate only numbers in data table column?

查看:61
本文介绍了如何仅验证数据表列中的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个数据表中有一个数据表,一列有产品编号(描述),并且有1,2,3,4,5之类的值,现在我想在此列中仅验证任何特殊字符的数值或这一列中的字母显示验证消息如何操作。



我尝试过:



我用过

I have one data table in this data table one column have "Product number"(description) and have values like 1,2,3,4,5 now i want to validate only numeric value in this column any special character or letters in this column show the validation messaging how to do it.

What I have tried:

I have used

Regex r = new Regex(@"[~`!@#$%^&*()-+=|\{}':;.,<>/?]");



但正则表达式仅验证布尔值。我使用的另一种方法


but regex is validate Boolean only. and another method i have used

string MyString = "[~`!@#$%^&*()-+=|\{}':;.,<>/?]";
string mystring2 = "[^a-zA-Z]";
if (ds.Tables[s].Rows[i][Columncountvalue].ToString().Trim() == mystring2 || if (ds.Tables[s].Rows[i][Columncountvalue].ToString().Trim() == MyString))
{
//throw error message
}



但这个节目无法识别的转义序列此错误消息。


But this show "Unrecognized escape sequence" this error message.

推荐答案

%^& *() - + = | \ {}':;。,<> / ?]);
%^&*()-+=|\{}':;.,<>/?]");



但正则表达式仅验证布尔值。我使用的另一种方法


but regex is validate Boolean only. and another method i have used

string MyString = "[~`!@#


%^& *() - + = | \ {}':;。,<> /?];
string mystring2 =[^ a-zA-Z];
if(ds.Tables [s] .Rows [i] [Columncountvalue] .ToString()。Trim()== mystring2 || if( ds.Tables [s] .Rows [i] [Columncountvalue] .ToString()。Trim()== MyString))
{
//抛出错误消息
}
%^&*()-+=|\{}':;.,<>/?]"; string mystring2 = "[^a-zA-Z]"; if (ds.Tables[s].Rows[i][Columncountvalue].ToString().Trim() == mystring2 || if (ds.Tables[s].Rows[i][Columncountvalue].ToString().Trim() == MyString)) { //throw error message }



但这显示无法识别的转义序列这条错误信息。


But this show "Unrecognized escape sequence" this error message.


我不知道你为什么需要正则表达式来检查数据是否是一个nu mber与否。一种非常简单的方法是使用TryParse方法。例如,如果预期数字是整数,只需使用 Int32.TryParse方法(字符串,Int32)(系统) [ ^ ]



第二件事是,你为什么要首先测试这个?如果这是一个数据表列,只需将列数据类型定义为整数。如果您尝试设置的值不是整数,则会自动抛出异常。请查看 DataColumn.DataType属性(系统。数据) [ ^ ]
I'm not sure why you need regular expressions to check if the data is a number or not. One very simple way is to use TryParse method. For example if the number is expected to be an integer, simply use Int32.TryParse Method (String, Int32) (System)[^]

The second thing is, why do you need to test this in the first place? If this is a datatable column, just define the column data type as integer. If the value you try to set isn't an integer an exception is automatically thrown. Have a look at DataColumn.DataType Property (System.Data)[^]


这篇关于如何仅验证数据表列中的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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