zend验证器正则表达式遇到问题-整数或可选浮点数 [英] having problems with zend validator regex for a number - integer or optional float

查看:124
本文介绍了zend验证器正则表达式遇到问题-整数或可选浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很感谢能帮助我发疯的一些帮助.我正在尝试验证zend表单以整数或小数形式(2个小数位)形式提交的数字,允许输入0或0.00,但不能为空.

I'd really appreciate some help with this its been driving me nuts. I'm trying to validate numbers submitted by a zend form either in the form of integers or decimals (2 decimal places) allowing for 0 or 0.00 but not empty inputs.

$form->element->addValidator ('regex', false, array(
  'pattern'=>'/^\d+(\d{1,5})?(\.\d{1,2})?$/', 
  'messages'=>array(
   'regexInvalid'=>'required',
   'regexNotMatch'=>'number required')
  )
);

由于某种原因,像100.00这样的浮点数会生成以下验证错误消息: '100.00'包含不是数字的字符;但仅允许数字".不会生成regexNotMatch消息或regexInvalid消息,这是我期望的.

For some reason a float like 100.00 generates the following validation error message: "'100.00' contains characters which are not digits; but only digits are allowed". The regexNotMatch message or regexInvalid message aren't generated which is what I'd expect.

此验证错误消息似乎是由我未调用的数字验证程序生成的,是否可以做些什么来阻止它启动?

This validation error message seems to be generated by the digits validator which I'm not calling is there something I can do to stop it kicking in?

空的输入也不会生成任何验证错误消息.

Also empty inputs aren't generating any validation error messages.

如果我尝试使用类似"rt67"的字符串,则会正确显示regexNotMatch消息.

If I try a string like 'rt67' my regexNotMatch message is displayed correctly.

如果这有任何区别,我正在使用不带mvc的zend组件.

I'm using zend components without the mvc if this makes any difference.

推荐答案

我正在呼叫数字验证器,因此实际上没有问题.我一直在通过仅使用setRequired来接受空输入的方法.一整夜的睡眠可以做什么呢!但是,如果有人知道正则表达式为什么接受空输入,我将不胜感激.

I was calling the digit validator so there wasn't actually a problem with it. I've worked around the acceptance of empty inputs being accepted by simply using setRequired. Its amazing what a nights sleep can do! I would however be grateful if anyone knows why the regex accepted empty input.

这篇关于zend验证器正则表达式遇到问题-整数或可选浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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