Copy link to clipboard
Copied
Hi,
Is there a way to enforce the number of digits on a field? The following will not allow the field to be greater than 7. Is there a way to prevent the field from being less than 7?
<cfinput type="Text" name="name" message="This is a numeric field." validate="integer" required="No" size="7" maxlength="7">
Thanks!
Copy link to clipboard
Copied
Not with a single simple parameter.
But it is easy enough to do with client side JavaScript for the users benifit and server side code for security.
Copy link to clipboard
Copied
<cfinput> does range validation out of the box.
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Part_3_CFML_Ref_1.html
--
Adam
Copy link to clipboard
Copied
In this special-case, you can use numeric-range validation.
In the more general case, you can use regular expression matching (see the "mask" and "pattern" options of <cfinput>).
A regular-expression is a string pattern. You could therefore define a pattern for "six digits and nothing else."
Copy link to clipboard
Copied
jenn wrote:
Is there a way to enforce the number of digits on a field? The following will not allow the field to be greater than 7. Is there a way to prevent the field from being less than 7?
<cfinput type="Text" name="name" message="This is a numeric field." validate="integer" required="No" size="7" maxlength="7">
<cfinput type="Text" name="name" message="This is a numeric field. You must enter 7 digits." validate="regex" pattern="[0-9]{7}" required="yes" size="7" maxlength="7" mask="9999999">
Copy link to clipboard
Copied
Strictly speaking, the pattern might also include the "$" and "^" characters, to "anchor" the pattern to the beginning and to the end of the input string.
Otherwise, the pattern would match if a sequence of 6 digits occurred anywhere within the input.
Obviously, a length-check (and so on) is enough to preclude this case, in this particular example, but I am speaking to the general case. There are plenty of "regular-expression tester" web-sites available, and regular-expression implementations are generally comparable the world over.
Regular expressions are probably the most important and fundamental validation tool that you have, precisely because they are "generally applicable." You can use them not only to validate strings, but to extract substrings from them and to perform substring replacements. Almost every programming language now supports RE's, and you need to learn and know them well.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now