gpt4 book ai didi

javascript - 查询 : validate price float (7, 2)

转载 作者:行者123 更新时间:2023-11-30 15:57:18 26 4
gpt4 key购买 nike

我正在使用 jquery 验证插件。
我必须验证价格输入。
我数据库中的列是 float(7,2)所以我的价格接受

  • 最多 7 个数字,不带逗号或
  • 逗号前 5 个数字和逗号后 2 个(最大)

我已经用这个 php 代码在服务器端做了验证

if (strstr($price, '.') && (strlen(substr(strrchr($price, "."), 1)) > 2 || strlen($price) > 8 )  ) {
echo "invalid format";
}
elseif (!strstr($price, '.')) {
if(strlen($price) > 5 )
{
echo "invalid format";
}
}

我需要在客户端使用 jquery 验证插件添加新方法进行相同的验证。
但我找不到真正的正则表达式来做到这一点。

更新

详细说明:
我需要接受任何小于或等于 7 位数字的数字,如 (12 - 1234 - 1234567) .. - .. 以及逗号前小于或等于 5 位数字且逗号后小于或等于 2 位数字的任何数字,如 (123.1 - 12345.12 - 12.23)

最佳答案

Update

detail description : i need to accept any number less than or equal 7 digits like (12 - 1234 - 1234567 ) .. - .. and any number with less than or equal 5 digits before comma and less than or equal 2 digits before comma like (123.1 - 12345.12 - 12.23 )

应该"and less than or equal 2 digits before comma" and less than or equal 2 digits before comma"?


编辑、更新

您可以将 pattern 属性与 RegExp \d{1,5},\d{1,2}|\d{1,7} 一起使用

<form>
<input type="text"
pattern="\d{1,5},\d{1,2}|\d{1,7}"
title="a) any number less than or equal 7 digits; b) any number with less than or equal 5 digits before comma and less than or equal 2 digits after comma"
required />
<input type="submit" />
</form>

关于javascript - 查询 : validate price float (7, 2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38311833/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com