Skip to main content
Known Participant
November 13, 2013
質問

How to validate a series of 0s value

  • November 13, 2013
  • 返信数 1.
  • 526 ビュー

I'm trying validate values such as 0 or 00 or 000 or 0000, or 000000 up to 10 digits.

I can use <cfif varId IS "0" OR varId IS "00" OR varId IS "000" etc> but I believed there should be a better and more elegant way to do this.

I need to reject anyone of these value from entering my database table.

Can anyone help?

Thank you!

    このトピックへの返信は締め切られました。

    返信数 1

    Participating Frequently
    November 13, 2013

    use a regular expression.

    <cfif reFind("^0{1,10}$", varId)>

    There might be an issue here though, I can't remember if ColdFusion allows for 2-digit values in the { } quantifier part of that (not everywhere can deal with it, and Adobe CF's regex engine is old enough that I doubt it will manage).

    If you don't really care about the length (e.g. theoretically an 11 digit string of zeros is also invalid), then you could also just do:

    <cfif reFind("^0+$", varId)>

    Carl Von Stetten
    Legend
    November 13, 2013

    I just confirmed that the first expression @duncancumming provided will work on CF 9.0.2.  It validates strings containing 1-10 zeroes, but fails on 11 or more zeroes, as intended.

    -Carl V.