Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to validate a 6 digit field

Participant ,
Apr 01, 2010 Apr 01, 2010

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!

TOPICS
Getting started
2.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Apr 01, 2010 Apr 01, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 01, 2010 Apr 01, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 12, 2010 Apr 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."

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 13, 2010 Apr 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">

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 13, 2010 Apr 13, 2010
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources