How to validate characters ONLY in ColdFusion CFForm?

8.1k Views Asked by At

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

4

There are 4 best solutions below

0
On BEST ANSWER

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.

0
On

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.

0
On
<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.

3
On

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>