Hi all,

I have this preg_mach code

preg_match("/^([0-9]||_|-)+$/", $str)) ? FALSE : TRUE;

I'd like to know if someone can guide me how to add 15 to 20 digits.
What i want to say is if someone posts less than 15 digits and no more than 20 digits the form won't be validated.

Any help is apreciated.

Thanks.

    add {15,20} before the end of string delimiter.

      Derokorian;11013157 wrote:

      add {15,20} before the end of string delimiter.

      Hi, i did what you sayd ( atleast waht i've understood ) it's working but if the number has an - or _ it displays en error .

      This is how i modified based on your reply

      preg_match("/^([0-9]{15,20}||_|-)+$/", $str)) ? FALSE : TRUE;

      If i insert let's say 012345678901234 ( it's working ) but if i insert 012345-678901234 isn't working anymore .

      What i'm doing wrong ?

      Thank you in advance.

        The end-of-string delimiter in a regular expression is a dollar sign just before the ending pattern delimiter. Note that you should actually be replacing the '+' modifier near that same location rather than appending {15,20} after it.

        For a good resource on understanding regular expressions, one website I always refer to is: Regular-Expressions.info

          I give up for now.
          I've tried all the things i could and still not working.
          If 15 - 20 limit working ( dosen't work - ) if ( - work ) dosen't work 15-20 limit.

          Thank you for trying to help.

            I think both Derokorian and I misunderstood your description in your first post above. Let me try to clarify...

            What you're wanting is to check if the given value contains between 15 and 20 numerical digits, with any number of underscores or hyphens (including 0) located at any point within that string of numbers. Is that correct?

            If so, my guess at a pattern would be:

            /^(?:[0-9][_-]*){15,20}$/

            EDIT: Note that my pattern above stipulates that the string must begin with a numerical digit, but allows the string to end with a hyphen or underscore. If that latter bit is undesired, you could instead do something like:

            /^(?:[0-9][_-]*){14,19}[0-9]$/
              bradgrafelman;11013171 wrote:

              I think both Derokorian and I misunderstood your description in your first post above. Let me try to clarify...

              What you're wanting is to check if the given value contains between 15 and 20 numerical digits, with any number of underscores or hyphens (including 0) located at any point within that string of numbers. Is that correct?

              If so, my guess at a pattern would be:

              /^(?:[0-9][_-]*){15,20}$/

              EDIT: Note that my pattern above stipulates that the string must begin with a numerical digit, but allows the string to end with a hyphen or underscore. If that latter bit is undesired, you could instead do something like:

              /^(?:[0-9][_-]*){14,19}[0-9]$/

              Hi bradgrafelman,

              Thank you very much, you did it like i wanted.
              English isn't my native language that's why i didn't explained well what i wanted .

              I've noticed one more thing i'd like to modify , i'd like to replace with / ( here : [-] ) , i tried to replace _ with / [/-] and isn't working.

              The numbers could be as 1521026/99/081714/5 or 1521026-99-081714-501 ( it dosen't matter where the - or / are placed between the numbers.

              Thanks.

                bradgrafelman;11013171 wrote:

                I think both Derokorian and I misunderstood your description in your first post above. Let me try to clarify...

                What you're wanting is to check if the given value contains between 15 and 20 numerical digits, with any number of underscores or hyphens (including 0) located at any point within that string of numbers. Is that correct?

                If so, my guess at a pattern would be:

                /^(?:[0-9][_-]*){15,20}$/

                EDIT: Note that my pattern above stipulates that the string must begin with a numerical digit, but allows the string to end with a hyphen or underscore. If that latter bit is undesired, you could instead do something like:

                /^(?:[0-9][_-]*){14,19}[0-9]$/

                Hi bradgrafelman,

                Thank you very much, you did it like i wanted.
                English isn't my native language that's why i didn't explained well what i wanted .

                I've noticed one more thing i'd like to modify , i'd like to replace with / ( here : [-] ) , i tried to replace _ with / [/-] and isn't working.

                The numbers could be as 1521026/99/081714/5 or 1521026-99-081714-501 ( it dosen't matter where the - or / are placed between the numbers.

                Thanks.

                  The problem is that '/' is used as the pattern delimiter (i.e. it marks where the pattern begins and ends).

                  In order to use a literal '/' within the pattern, you either need to: 1) change the pattern delimiter, or 2) escape the literal instances by prepending them with a backslash (e.g. '\/'). Note that since option #2 introduces extra characters, I personally always try to go with option #1 (with '@' or '#' often being my secondary choices for pattern delimiters).

                    Hi bradgrafelman,

                    Since first choice is harder to understand i choosed second choise and this change works for my needs

                    [\/-]

                    .

                    Thank you again , you really helped me.

                      Write a Reply...