Use regex to do validation in g:TextField

∥☆過路亽.° 提交于 2019-12-07 16:02:10

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/22725035/use-regex-to-do-validation-in-gtextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!