How to check characters only in ColdFusion CFForm?

I have a very simple cfform with one 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 AZ and az in any combination and have some content in it.

In practice, I can enter 'a' and the javascript check does not complain. Since the space character is not in AZ and not in az, what happens?

Thanks! Chris

+4
source share
4 answers

You are missing the anchors of the beginning and end of the line:

 ^[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 these anchors, the regular expression just needs to match anywhere, it doesn't need to be fully matched.

+13
source

personally, I would avoid using the built-in coldfusion JavaScript code. You will have much more control if you refuse on your own, and this will give you the opportunity to display errors in other ways than the warning field.

 <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> 
+2
source
 <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 also enter and indicate, but how should I do to remake รค รฅ รถ.

Of course, thanks for the help. You help so much.

+1
source

Came to this article when I was looking for my solution. A non-intrusive way is needed so that people do not enter anything by numbers in one field and nothing but letters, numbers or spaces in other fields. I could not use pattern = "9999" for numbers, as this was not a required field, and people "screamed" if they inserted this field. Likewise, it was not possible to use pattern = "xxx" for alpha / number fields, as I also needed spaces.

Leapfrogging from this article and using javascript that was developed by previous programmers for this client, I came up with these beautiful handlers, thought that I would share if someone needed this elegant solution, and ALSO, because sometimes I forget and will find it again.

Either in the .js file that 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 fields or cfinput will have OnKeyUp="numChecker(this)" or OnKeyUp="charChecker(this)" in their attributes.

As people type, if they enter an invalid character, this script will delete and simply delete that bad character. No additional buttons for clicks or alerts are required.

+1
source

All Articles