HTML输入数值的字段模式。

时间:2022-11-16 07:45:40

I have an input field that I am using to get a monetary amount from a user. I am using a jQuery Money Mask function to put a mask over the user's input. So for example if the user types "100000" it will show in the input field as "$ 1,000.00". The issue is that I have a pattern setup for the input field to restrict it to only numerical values. I need to modify this regular expression (pattern="[0-9]*") so that it accepts monetary strings. Any help is most appreciated!

我有一个输入字段,我用来从用户那里获得金额。我正在使用jQuery Money Mask功能将掩码放在用户的输入上。因此,例如,如果用户键入“100000”,它将在输入字段中显示为“$ 1,000.00”。问题是我有一个输入字段的模式设置,只能将其限制为数值。我需要修改这个正则表达式(pattern =“[0-9] *”),以便它接受货币字符串。任何帮助都非常感谢!

Code for the input field:

输入字段的代码:

<input name="rentPayment[paymentAmount]" id="paymentAmount" placeholder="0.00" value="" type="text" pattern="[0-9]*">

2 个解决方案

#1


3  

I am not familiar with JQuery Money Mask but if all you need is regex then following should be enough to allow monetary values.

我不熟悉JQuery Money Mask,但如果你需要的只是正则表达式,那么关注应该足以允许货币价值。

pattern="[0-9$,.]*" - would allow 0-9 dollar sign, comma and period zero or more times.

#2


2  

How about:

\$(\d(,?\d+)*)+(\.\d+)?

This will support:

这将支持:

$324234.23423
$3,242,343
$3,242.4234
$3242

$ 324234.23423 $ 3,242,343 $ 3,242.4234 $ 3242

But I wouldn't suggest this approach, since it will depend on the user's localisation, and it also has a dependency on the plugin - which is really just masking the user's input. I would prefer to validate the user's string as you have done, and let the masking plugin do it's work of prettifying. So you need to do your validation before the plugin takes place.

但我不会建议这种方法,因为它将取决于用户的本地化,并且它还依赖于插件 - 这实际上只是掩盖了用户的输入。我更喜欢像你一样验证用户的字符串,并让屏蔽插件完成它的美化工作。因此,您需要在插件发生之前进行验证。

A feasible solution for this would be to leave the validation field blank - Money Mask will be validating for you, so you don't need an external pattern to do further validation in this case.

一个可行的解决方案是将验证字段留空 - Money Mask将为您验证,因此在这种情况下您不需要外部模式进行进一步验证。

#1


3  

I am not familiar with JQuery Money Mask but if all you need is regex then following should be enough to allow monetary values.

我不熟悉JQuery Money Mask,但如果你需要的只是正则表达式,那么关注应该足以允许货币价值。

pattern="[0-9$,.]*" - would allow 0-9 dollar sign, comma and period zero or more times.

#2


2  

How about:

\$(\d(,?\d+)*)+(\.\d+)?

This will support:

这将支持:

$324234.23423
$3,242,343
$3,242.4234
$3242

$ 324234.23423 $ 3,242,343 $ 3,242.4234 $ 3242

But I wouldn't suggest this approach, since it will depend on the user's localisation, and it also has a dependency on the plugin - which is really just masking the user's input. I would prefer to validate the user's string as you have done, and let the masking plugin do it's work of prettifying. So you need to do your validation before the plugin takes place.

但我不会建议这种方法,因为它将取决于用户的本地化,并且它还依赖于插件 - 这实际上只是掩盖了用户的输入。我更喜欢像你一样验证用户的字符串,并让屏蔽插件完成它的美化工作。因此,您需要在插件发生之前进行验证。

A feasible solution for this would be to leave the validation field blank - Money Mask will be validating for you, so you don't need an external pattern to do further validation in this case.

一个可行的解决方案是将验证字段留空 - Money Mask将为您验证,因此在这种情况下您不需要外部模式进行进一步验证。