Skip to main content
Inspiring
April 1, 2010
Question

How to validate a 6 digit field

  • April 1, 2010
  • 4 replies
  • 2592 views

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!

This topic has been closed for replies.

4 replies

Inspiring
April 13, 2010

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.

BKBK
Community Expert
Community Expert
April 13, 2010

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">

Inspiring
April 12, 2010

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."

ilssac
Inspiring
April 1, 2010

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.

Inspiring
April 1, 2010

<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