如何仅在 ColdFusion CFForm 中验证字符?

发布于 2024-07-26 12:03:40 字数 497 浏览 6 评论 0原文

我有一个非常简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

诗酒趁年少 2024-08-02 12:03:40

您缺少字符串开头和字符串结尾锚点:

^[A-Za-z]$

或者更有可能:

^[A-Za-z]{1,20}$

您的示例已修改:

<cfform action="asdf.cfm" method="post">
  <cfinput name="fieldName" type="text" size="20" maxlength="20" required="yes" validate="regex" pattern="^[A-Za-z]{1,20}$" message="Only characters are allowed." />
  <input type="submit" name="btnSubmit" value="check" />
</cfform>

如果没有这些锚点,正则表达式只需要匹配任何地方,它不需要完全匹配

You are missing the start-of-string and end-of-string anchors:

^[A-Za-z]$

or, more likely:

^[A-Za-z]{1,20}$

Your sample, modified:

<cfform action="asdf.cfm" method="post">
  <cfinput name="fieldName" type="text" size="20" maxlength="20" required="yes" validate="regex" pattern="^[A-Za-z]{1,20}$" message="Only characters are allowed." />
  <input type="submit" name="btnSubmit" value="check" />
</cfform>

Without those anchors, the regex merely needs to match anywhere, it does not need to match entirely.

深居我梦 2024-08-02 12:03:40

我个人会避免使用内置的 Coldfusion javascript。 如果您自己推出,您将拥有更多的控制权,并且它将使您能够以警报框以外的其他方式显示错误。

<script>
function checkit() {
    var v = document.getElementById("text1").value;
    if(!v.match(/^[a-zA-Z]+$/)) {
        alert(v + ' contains invalid characters');
        return false;
    }
    return true;
}
</script>
<form onsubmit="return checkit()">
<input type="text" id="text1">
<input type="submit">
</form>

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.

<script>
function checkit() {
    var v = document.getElementById("text1").value;
    if(!v.match(/^[a-zA-Z]+$/)) {
        alert(v + ' contains invalid characters');
        return false;
    }
    return true;
}
</script>
<form onsubmit="return checkit()">
<input type="text" id="text1">
<input type="submit">
</form>
送君千里 2024-08-02 12:03:40
<script>
function checkit() {
    var v = document.getElementById("text1").value;
    if(!v.match(/^[a-zA-Z\\ \\.\\]+$/)) {
        alert(v + ' contains invalid characters');
        return false;
    }
    return true;
}
</script>
<form onsubmit="return checkit()">
<input type="text" id="text1">
<input type="submit">
</form>

这里也可以输入和点,但是我必须如何做才能重新确认。

当然感谢所有的帮助,你帮助很大。

<script>
function checkit() {
    var v = document.getElementById("text1").value;
    if(!v.match(/^[a-zA-Z\\ \\.\\]+$/)) {
        alert(v + ' contains invalid characters');
        return false;
    }
    return true;
}
</script>
<form onsubmit="return checkit()">
<input type="text" id="text1">
<input type="submit">
</form>

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.

_蜘蛛 2024-08-02 12:03:40

当我寻找自己的解决方案时看到这篇文章。 需要一种非侵入性的方式来防止人们在一个字段中输入任何数字,而在其他字段中只输入字母、数字或空格。 我不能对数字使用pattern=“9999”,因为它不是必填字段,如果人们通过该字段进行制表符操作,就会被“大喊大叫”。 同样,无法对字母/数字字段使用pattern=“xxx”,因为我还需要允许空格。

从这篇文章开始,使用以前的程序员为该客户端开发的 javascript,我想出了这些漂亮的处理程序,我想我会分享,以防其他人需要这个优雅的解决方案,而且因为有时我会忘记,并且能够找到这个再次。

在包含或包含在标签中的 .js 文件中:

    function numChecker(e)
    {
      if (!e.value.match(/^[0-9]+$/))
      {
        e.value = e.value.substring(0.e.value.length -1 );
        e.focus();
        return false;
      }
      return true;
    }
    function charChecker(e)
    {
      if (!e.value.match(/^[a-zA-Z0-9\ ]+$/))
      {
        e.value = e.value.substring(0.e.value.length-1);
        e.focus();
        return false;
      }
      return true;
    }

然后您的输入或 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:

    function numChecker(e)
    {
      if (!e.value.match(/^[0-9]+$/))
      {
        e.value = e.value.substring(0.e.value.length -1 );
        e.focus();
        return false;
      }
      return true;
    }
    function charChecker(e)
    {
      if (!e.value.match(/^[a-zA-Z0-9\ ]+$/))
      {
        e.value = e.value.substring(0.e.value.length-1);
        e.focus();
        return false;
      }
      return true;
    }

Then your input or cfinput fields would have OnKeyUp="numChecker(this)" or OnKeyUp="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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文