有效的2位数十进制数字的正则表达式 [英] regular expression for valid 2 digit decimal number

查看:213
本文介绍了有效的2位数十进制数字的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在php中进行价格为100或100.45的验证 小数点后两位是可选的.

I want to have a validation in php for price which can be 100 or 100.45 The 2 decimal places will be optional.

现在验证应只允许数字.

Now the validation should allow only digits.

到目前为止,我设法实现了目标

So far i managed to achieve it

if (!preg_match('/^[0-9]+(\.[0-9]{1,2})?/', "100"))
{
    echo "Invalid";
}
else
{
    echo "Valid";
}

但是这里的问题是,即使我输入100a.00或100a或100.a00,它也显示有效

but the issue here is that it is showing valid even if i enter 100a.00 or 100a or 100.a00

请帮助我修复该问题,以便只允许使用100或100.00格式的数字

Please help me in fixing it so that only digits are allowed i.e 100 or 100.00 format

推荐答案

尝试一下:

if (!preg_match('/^[0-9]+(\.[0-9]{1,2})?$/', "100"))

$表示字符串结尾": http://www. php.net/manual/en/regexp.reference.meta.php

The $ denotes the "end of a string": http://www.php.net/manual/en/regexp.reference.meta.php

这篇关于有效的2位数十进制数字的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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