Minimum Phone numbers digits Woocomerce Checkout












2















I want the user to enter a minimum of 7 numbers for their phone number in the Woocommerce checkout. The function below works but it includes spaces, so if user enter 6 numbers with 1 space it counts it as 7. How Can I change the function so that it only counts numbers not spaces so that the user has to enter a minimum of 7 numbers.



// validation for Billing Phone checkout field
add_action('woocommerce_checkout_process', 'custom_validate_billing_phone');
function custom_validate_billing_phone() {
$is_correct = preg_match('/^[0-9 -]{7}/i', $_POST['billing_phone']);
if ( $_POST['billing_phone'] && !$is_correct) {
wc_add_notice( __( 'Phone Number must be <strong>minimum 7 numbers</strong>.' ), 'error' );
}
}









share|improve this question





























    2















    I want the user to enter a minimum of 7 numbers for their phone number in the Woocommerce checkout. The function below works but it includes spaces, so if user enter 6 numbers with 1 space it counts it as 7. How Can I change the function so that it only counts numbers not spaces so that the user has to enter a minimum of 7 numbers.



    // validation for Billing Phone checkout field
    add_action('woocommerce_checkout_process', 'custom_validate_billing_phone');
    function custom_validate_billing_phone() {
    $is_correct = preg_match('/^[0-9 -]{7}/i', $_POST['billing_phone']);
    if ( $_POST['billing_phone'] && !$is_correct) {
    wc_add_notice( __( 'Phone Number must be <strong>minimum 7 numbers</strong>.' ), 'error' );
    }
    }









    share|improve this question



























      2












      2








      2








      I want the user to enter a minimum of 7 numbers for their phone number in the Woocommerce checkout. The function below works but it includes spaces, so if user enter 6 numbers with 1 space it counts it as 7. How Can I change the function so that it only counts numbers not spaces so that the user has to enter a minimum of 7 numbers.



      // validation for Billing Phone checkout field
      add_action('woocommerce_checkout_process', 'custom_validate_billing_phone');
      function custom_validate_billing_phone() {
      $is_correct = preg_match('/^[0-9 -]{7}/i', $_POST['billing_phone']);
      if ( $_POST['billing_phone'] && !$is_correct) {
      wc_add_notice( __( 'Phone Number must be <strong>minimum 7 numbers</strong>.' ), 'error' );
      }
      }









      share|improve this question
















      I want the user to enter a minimum of 7 numbers for their phone number in the Woocommerce checkout. The function below works but it includes spaces, so if user enter 6 numbers with 1 space it counts it as 7. How Can I change the function so that it only counts numbers not spaces so that the user has to enter a minimum of 7 numbers.



      // validation for Billing Phone checkout field
      add_action('woocommerce_checkout_process', 'custom_validate_billing_phone');
      function custom_validate_billing_phone() {
      $is_correct = preg_match('/^[0-9 -]{7}/i', $_POST['billing_phone']);
      if ( $_POST['billing_phone'] && !$is_correct) {
      wc_add_notice( __( 'Phone Number must be <strong>minimum 7 numbers</strong>.' ), 'error' );
      }
      }






      php regex wordpress woocommerce phone-number






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 13:16









      LoicTheAztec

      87.7k1364101




      87.7k1364101










      asked Nov 23 '18 at 3:38









      DarrenLeeDarrenLee

      678




      678
























          3 Answers
          3






          active

          oldest

          votes


















          1














          I figured out the solution.



          $is_correct = preg_match('/^[0-9D]{7}/i', $_POST['billing_phone']);





          share|improve this answer


























          • You should accept you own answer if it works…

            – LoicTheAztec
            Nov 23 '18 at 13:17











          • This will still Count a Space as one of the seven characters.

            – Poul Bak
            Nov 23 '18 at 13:34



















          0














          $is_correct = preg_match('/^[0-9-]{7}/i', $_POST['billing_phone']);



          should work






          share|improve this answer
























          • Thank you, Unfortunately it does not work, if i add 7 numbers with a space in between the error notification appears. It needs to allow 7 number if their is a space in between as it should be 7 minimum even with spaces.

            – DarrenLee
            Nov 23 '18 at 5:00











          • try doing this to the evaluated string first $str = preg_replace('/s+/', '',$_POST['billing_phone']); then evaluate $str with the preg_match above. That will remove all white space from the field before you validate.

            – Hans-Eric Lippke
            Nov 23 '18 at 15:11





















          0














          You can use the following regex. It only Count the digits:



          ^(?:D*?dD*?){7}$


          It starts from start of string, then creates a non capturing Group, that matches
          optional any number of non digit, digit and optional any number of non digit. It will require exactly 7 of that.



          This means, it only Counts the digits and ignore non digits in the Count.



          Examples of match:



          1234567
          1 23 45 67
          123-4567


          Example of non-match:



          123 456
          12-34-56-78





          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440386%2fminimum-phone-numbers-digits-woocomerce-checkout%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            I figured out the solution.



            $is_correct = preg_match('/^[0-9D]{7}/i', $_POST['billing_phone']);





            share|improve this answer


























            • You should accept you own answer if it works…

              – LoicTheAztec
              Nov 23 '18 at 13:17











            • This will still Count a Space as one of the seven characters.

              – Poul Bak
              Nov 23 '18 at 13:34
















            1














            I figured out the solution.



            $is_correct = preg_match('/^[0-9D]{7}/i', $_POST['billing_phone']);





            share|improve this answer


























            • You should accept you own answer if it works…

              – LoicTheAztec
              Nov 23 '18 at 13:17











            • This will still Count a Space as one of the seven characters.

              – Poul Bak
              Nov 23 '18 at 13:34














            1












            1








            1







            I figured out the solution.



            $is_correct = preg_match('/^[0-9D]{7}/i', $_POST['billing_phone']);





            share|improve this answer















            I figured out the solution.



            $is_correct = preg_match('/^[0-9D]{7}/i', $_POST['billing_phone']);






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 23 '18 at 13:15









            LoicTheAztec

            87.7k1364101




            87.7k1364101










            answered Nov 23 '18 at 5:28









            DarrenLeeDarrenLee

            678




            678













            • You should accept you own answer if it works…

              – LoicTheAztec
              Nov 23 '18 at 13:17











            • This will still Count a Space as one of the seven characters.

              – Poul Bak
              Nov 23 '18 at 13:34



















            • You should accept you own answer if it works…

              – LoicTheAztec
              Nov 23 '18 at 13:17











            • This will still Count a Space as one of the seven characters.

              – Poul Bak
              Nov 23 '18 at 13:34

















            You should accept you own answer if it works…

            – LoicTheAztec
            Nov 23 '18 at 13:17





            You should accept you own answer if it works…

            – LoicTheAztec
            Nov 23 '18 at 13:17













            This will still Count a Space as one of the seven characters.

            – Poul Bak
            Nov 23 '18 at 13:34





            This will still Count a Space as one of the seven characters.

            – Poul Bak
            Nov 23 '18 at 13:34













            0














            $is_correct = preg_match('/^[0-9-]{7}/i', $_POST['billing_phone']);



            should work






            share|improve this answer
























            • Thank you, Unfortunately it does not work, if i add 7 numbers with a space in between the error notification appears. It needs to allow 7 number if their is a space in between as it should be 7 minimum even with spaces.

              – DarrenLee
              Nov 23 '18 at 5:00











            • try doing this to the evaluated string first $str = preg_replace('/s+/', '',$_POST['billing_phone']); then evaluate $str with the preg_match above. That will remove all white space from the field before you validate.

              – Hans-Eric Lippke
              Nov 23 '18 at 15:11


















            0














            $is_correct = preg_match('/^[0-9-]{7}/i', $_POST['billing_phone']);



            should work






            share|improve this answer
























            • Thank you, Unfortunately it does not work, if i add 7 numbers with a space in between the error notification appears. It needs to allow 7 number if their is a space in between as it should be 7 minimum even with spaces.

              – DarrenLee
              Nov 23 '18 at 5:00











            • try doing this to the evaluated string first $str = preg_replace('/s+/', '',$_POST['billing_phone']); then evaluate $str with the preg_match above. That will remove all white space from the field before you validate.

              – Hans-Eric Lippke
              Nov 23 '18 at 15:11
















            0












            0








            0







            $is_correct = preg_match('/^[0-9-]{7}/i', $_POST['billing_phone']);



            should work






            share|improve this answer













            $is_correct = preg_match('/^[0-9-]{7}/i', $_POST['billing_phone']);



            should work







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 23 '18 at 4:50









            guest is my nameguest is my name

            1




            1













            • Thank you, Unfortunately it does not work, if i add 7 numbers with a space in between the error notification appears. It needs to allow 7 number if their is a space in between as it should be 7 minimum even with spaces.

              – DarrenLee
              Nov 23 '18 at 5:00











            • try doing this to the evaluated string first $str = preg_replace('/s+/', '',$_POST['billing_phone']); then evaluate $str with the preg_match above. That will remove all white space from the field before you validate.

              – Hans-Eric Lippke
              Nov 23 '18 at 15:11





















            • Thank you, Unfortunately it does not work, if i add 7 numbers with a space in between the error notification appears. It needs to allow 7 number if their is a space in between as it should be 7 minimum even with spaces.

              – DarrenLee
              Nov 23 '18 at 5:00











            • try doing this to the evaluated string first $str = preg_replace('/s+/', '',$_POST['billing_phone']); then evaluate $str with the preg_match above. That will remove all white space from the field before you validate.

              – Hans-Eric Lippke
              Nov 23 '18 at 15:11



















            Thank you, Unfortunately it does not work, if i add 7 numbers with a space in between the error notification appears. It needs to allow 7 number if their is a space in between as it should be 7 minimum even with spaces.

            – DarrenLee
            Nov 23 '18 at 5:00





            Thank you, Unfortunately it does not work, if i add 7 numbers with a space in between the error notification appears. It needs to allow 7 number if their is a space in between as it should be 7 minimum even with spaces.

            – DarrenLee
            Nov 23 '18 at 5:00













            try doing this to the evaluated string first $str = preg_replace('/s+/', '',$_POST['billing_phone']); then evaluate $str with the preg_match above. That will remove all white space from the field before you validate.

            – Hans-Eric Lippke
            Nov 23 '18 at 15:11







            try doing this to the evaluated string first $str = preg_replace('/s+/', '',$_POST['billing_phone']); then evaluate $str with the preg_match above. That will remove all white space from the field before you validate.

            – Hans-Eric Lippke
            Nov 23 '18 at 15:11













            0














            You can use the following regex. It only Count the digits:



            ^(?:D*?dD*?){7}$


            It starts from start of string, then creates a non capturing Group, that matches
            optional any number of non digit, digit and optional any number of non digit. It will require exactly 7 of that.



            This means, it only Counts the digits and ignore non digits in the Count.



            Examples of match:



            1234567
            1 23 45 67
            123-4567


            Example of non-match:



            123 456
            12-34-56-78





            share|improve this answer






























              0














              You can use the following regex. It only Count the digits:



              ^(?:D*?dD*?){7}$


              It starts from start of string, then creates a non capturing Group, that matches
              optional any number of non digit, digit and optional any number of non digit. It will require exactly 7 of that.



              This means, it only Counts the digits and ignore non digits in the Count.



              Examples of match:



              1234567
              1 23 45 67
              123-4567


              Example of non-match:



              123 456
              12-34-56-78





              share|improve this answer




























                0












                0








                0







                You can use the following regex. It only Count the digits:



                ^(?:D*?dD*?){7}$


                It starts from start of string, then creates a non capturing Group, that matches
                optional any number of non digit, digit and optional any number of non digit. It will require exactly 7 of that.



                This means, it only Counts the digits and ignore non digits in the Count.



                Examples of match:



                1234567
                1 23 45 67
                123-4567


                Example of non-match:



                123 456
                12-34-56-78





                share|improve this answer















                You can use the following regex. It only Count the digits:



                ^(?:D*?dD*?){7}$


                It starts from start of string, then creates a non capturing Group, that matches
                optional any number of non digit, digit and optional any number of non digit. It will require exactly 7 of that.



                This means, it only Counts the digits and ignore non digits in the Count.



                Examples of match:



                1234567
                1 23 45 67
                123-4567


                Example of non-match:



                123 456
                12-34-56-78






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 23 '18 at 13:47

























                answered Nov 23 '18 at 13:40









                Poul BakPoul Bak

                5,46831232




                5,46831232






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440386%2fminimum-phone-numbers-digits-woocomerce-checkout%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Wiesbaden

                    Marschland

                    Dieringhausen