如何仅在 ColdFusion CFForm 中验证字符?
我有一个非常简单的 cfform,只有一个表单字段:
<cfform action="asdf.cfm" method="post">
<cfinput name="fieldName" type="text" size="20" maxlength="20" required="yes" validate="regex" pattern="[A-Za-z]" message="Only characters are allowed." />
<input type="submit" name="btnSubmit" value="check" />
</cfform>
理论上,这将只允许 AZ 和 az 的任意组合,并且其中必须包含一些内容。
实际上,我可以输入“a a”并且 javascript 验证不会抱怨。 由于“空格”字符不在 AZ 中,也不在 az 中,这是怎么回事?
谢谢! 克里斯
I've got a very simple cfform with a single form field:
<cfform action="asdf.cfm" method="post">
<cfinput name="fieldName" type="text" size="20" maxlength="20" required="yes" validate="regex" pattern="[A-Za-z]" message="Only characters are allowed." />
<input type="submit" name="btnSubmit" value="check" />
</cfform>
Theoretically, this would allow ONLY A-Z and a-z in any combination, and must have some content in it.
In practice, I'm able to enter 'a a' and the javascript validation does not complain. Since the 'space' character is not in A-Z and not in a-z, what's going on?
Thanks!
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您缺少字符串开头和字符串结尾锚点:
或者更有可能:
您的示例已修改:
如果没有这些锚点,正则表达式只需要匹配任何地方,它不需要完全匹配。
You are missing the start-of-string and end-of-string anchors:
or, more likely:
Your sample, modified:
Without those anchors, the regex merely needs to match anywhere, it does not need to match entirely.
我个人会避免使用内置的 Coldfusion javascript。 如果您自己推出,您将拥有更多的控制权,并且它将使您能够以警报框以外的其他方式显示错误。
personally I would avoid using the built in coldfusion javascript. You will have much more control if you roll your own and it will give you the ability to display errors in other ways than an alert box.
这里也可以输入和点,但是我必须如何做才能重新确认。
当然感谢所有的帮助,你帮助很大。
Here is also enter and point possible, but how I'm must do to reconoce ä å ö.
Of course thanks for all help, You help so much.
当我寻找自己的解决方案时看到这篇文章。 需要一种非侵入性的方式来防止人们在一个字段中输入任何数字,而在其他字段中只输入字母、数字或空格。 我不能对数字使用pattern=“9999”,因为它不是必填字段,如果人们通过该字段进行制表符操作,就会被“大喊大叫”。 同样,无法对字母/数字字段使用pattern=“xxx”,因为我还需要允许空格。
从这篇文章开始,使用以前的程序员为该客户端开发的 javascript,我想出了这些漂亮的处理程序,我想我会分享,以防其他人需要这个优雅的解决方案,而且因为有时我会忘记,并且能够找到这个再次。
在包含或包含在标签中的 .js 文件中:
然后您的输入或 cfinput 字段将具有
OnKeyUp="numChecker(this)"
或OnKeyUp="charChecker(this)" 在它们的属性中。
当人们输入时,如果他们输入无效字符,该脚本将启动并简单地删除该错误字符。 无需点击额外的按钮,也无需关闭警报。
Came across this article while I was searching for a solution of my own. Needed a non-intrusive way to keep people from entering anything by numbers in one field and nothing but letters, numbers or spaces in other fields. I couldn't use pattern="9999" for the numbers as it was not a required field and folks get "yelled at" if they tab through that field. Likewise, could not use pattern="xxx" for the alpha/numeric fields as I also needed to allow spaces.
Leapfrogging from this article and using javascript that previous programmers had developed for that client, I came up with these beautiful handlers, thought I'd share in case someone else needed this elegant solution and ALSO because sometimes I forget and would be able to find this again.
Either in a .js file you include or enclosed in tags:
Then your input or cfinput fields would have
OnKeyUp="numChecker(this)"
orOnKeyUp="charChecker(this)"
in their attributes.As people type, if they enter an invalid character this script will kick in and simply remove that bad character. No extra buttons to click or alerts to dismiss.