Minimum Phone numbers digits Woocomerce Checkout
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
add a comment |
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
add a comment |
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
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
php regex wordpress woocommerce phone-number
edited Nov 23 '18 at 13:16
LoicTheAztec
87.7k1364101
87.7k1364101
asked Nov 23 '18 at 3:38
DarrenLeeDarrenLee
678
678
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
I figured out the solution.
$is_correct = preg_match('/^[0-9D]{7}/i', $_POST['billing_phone']);
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
add a comment |
$is_correct = preg_match('/^[0-9-]{7}/i', $_POST['billing_phone']);
should work
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
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
I figured out the solution.
$is_correct = preg_match('/^[0-9D]{7}/i', $_POST['billing_phone']);
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
add a comment |
I figured out the solution.
$is_correct = preg_match('/^[0-9D]{7}/i', $_POST['billing_phone']);
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
add a comment |
I figured out the solution.
$is_correct = preg_match('/^[0-9D]{7}/i', $_POST['billing_phone']);
I figured out the solution.
$is_correct = preg_match('/^[0-9D]{7}/i', $_POST['billing_phone']);
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
add a comment |
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
add a comment |
$is_correct = preg_match('/^[0-9-]{7}/i', $_POST['billing_phone']);
should work
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
add a comment |
$is_correct = preg_match('/^[0-9-]{7}/i', $_POST['billing_phone']);
should work
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
add a comment |
$is_correct = preg_match('/^[0-9-]{7}/i', $_POST['billing_phone']);
should work
$is_correct = preg_match('/^[0-9-]{7}/i', $_POST['billing_phone']);
should work
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 23 '18 at 13:47
answered Nov 23 '18 at 13:40
Poul BakPoul Bak
5,46831232
5,46831232
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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