使用正则表达式在g:TextField中进行验证 [英] Use regex to do validation in g:TextField

查看:109
本文介绍了使用正则表达式在g:TextField中进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究grails应用程序,并希望能够在g:textField上进行验证。我只想确保数字是数字,单词是单词。我想避免通过JS做到这一点,但如果这是唯一的方法,那就这样吧。这是我正在尝试的。

 < g:textField type =numberpattern =^(?:[ -  +]?[1-9 ] \ d * | 0)?(?: \.\d +)?\ $class =form-controlname =hoursvalue =$ {hours}/>< DIV> 

任何建议或指示?或者我应该只是通过JS做到这一点?

解决方案

所以我能够找出正确的正则表达式。

 < g:textField type = numberpattern =^ [0-9] + \\s * \ $ | ^ [0-9] + \\。[0-9] + \\s * \ $required =trueclass =form-controlname =hoursvalue =$ {deliverable.estimatedHours}/> 

现在将检查输入字符串的格式并确保它只包含数字。 p>

I am working on a grails applications and want the ability to do validation on a g:textField. I just want to make sure that numbers are numbers and words are words. I would like to avoid doing it through JS but if that is the only way, then so be it. Here is what I am trying out.

<g:textField type="number" pattern="^(?:[-+]?[1-9]\d*|0)?(?:\.\d+)?\$" class="form-control" name="hours" value="${hours}"/></div>

Any suggestions or directions? Or should I just do it through JS?

解决方案

So I was able to figure out the correct regex. The issue that I was having was about the escape characters and the way they were being interoperated.

<g:textField type="number" pattern="^[0-9]+\\s*\$|^[0-9]+\\.?[0-9]+\\s*\$" required="true" class="form-control" name="hours" value="${deliverable.estimatedHours}"/>

This will now check the format for an input string and make sure that it only contains numbers.

这篇关于使用正则表达式在g:TextField中进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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