数字大于0的验证(正则表达式) [英] validation for number greater than 0 (in regular expression)

查看:3744
本文介绍了数字大于0的验证(正则表达式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用正则表达式验证器...我想检查数字是否大于零..如果0表示它不会进入..这是我的验证器..检查并提出解决方案..

 <   asp:RegularExpressionValidator     ID   =  revAvailablePeriod    runat   = 服务器    ErrorMessage   = 必须大于0    ControlToValidate   =  txtQuantity     ValidationExpression   =  ^ [1-9] [0-9] * $    SetFocusOnError   =  true    ValidationGroup   =  AddAssests   >  





在这不能键入0它的oky ...但我必须键入0.2,0.25,0.12等。(因为它大于零正确!...)..



我想输入小于零的十进制数。请帮助..

解决方案

SetFocusOnError = true ValidationGroup = AddAssests >





在此不能输入0其oky ..但是我必须键入0.2,0.25,0.12等。(因为它大于零正确!...)..



我想要类型十进制也比零更重要。所以请帮助..


在这种情况下,使用正则表达式是一个坏主意。



做完全不同的事情。首先,如果您使用的是文本框,则可以过滤掉输入,这样用户只能输入数字(和退格键,也称为字符,#8)。在您的情况下,使用JavaScript很容易做到这一点。当您需要使用该值时,请执行验证。更一般的形式,如果是这样的话:

  static   bool 验证(字符串输入, uint  validMininum, uint  validMaximum, out   uint   value ){
bool valid = uint .TryParse(输入, out value );
如果(有效)
有效= validMininum < = value && value < = validMaximum;
返回有效;
}





我假设 uint ,因为你不想要考虑负值,但你可以使用任何其他数字类型,例如, ulong



-SA


试试这个正则表达式 ^ [1-9] [0-9] *(\。[0-9] + )|?0 + \ [0-9] * [1-9] [0-9] *

Hi,
I am using a regular expression validator ...I want to check if number is greater than zero ..if 0 means it will not enter ..here is my validator ..check it and suggest a solution..

<asp:RegularExpressionValidator ID="revAvailablePeriod" runat="server" ErrorMessage="Must be greater than 0" ControlToValidate="txtQuantity" ValidationExpression="^[1-9][0-9]*$" SetFocusOnError="true" ValidationGroup="AddAssests" >



in this can't type 0 its oky...but I have to type 0.2, 0.25 ,0.12 etc. .(since it greater than zero right!...)..

I want to type decimal also that grater than zero. So please help..

解决方案

" SetFocusOnError="true" ValidationGroup="AddAssests" >



in this can't type 0 its oky...but I have to type 0.2, 0.25 ,0.12 etc. .(since it greater than zero right!...)..

I want to type decimal also that grater than zero. So please help..


In this very case, using Regular Expression is a bad idea.

Do completely different thing. First of all, if you are using a text box, you can filter out input so the user could enter only numbers (and backspace, which is also considered a character, #8). This is easy to do with JavaScript, in your case. And when you need to use the value, perform validation. In a bit more general form, if will be something like this:

static bool Validate(string input, uint validMininum, uint validMaximum, out uint value) {
    bool valid = uint.TryParse(input, out value);
    if (valid)
       valid = validMininum <= value && value <= validMaximum;
    return valid;
}



I assumed uint, because you did not want to consider negative values, but you can use any other numeric type, for example, ulong.

—SA


Try this regular expression ^[1-9][0-9]*(\.[0-9]+)?|0+\.[0-9]*[1-9][0-9]*


这篇关于数字大于0的验证(正则表达式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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