Search code examples
phpwordpressmaxlength

Set the character length in the field


can anyone help me with this: Namely, I want to set the length of the character for the Phone field in registration, and in the case if it has more than 10 characters, to display a mistake.

Form: enter image description here

in this way, I can already take the value from the Phone field:

/themes/functions.php

add_action('woocommerce_created_customer', 'handle_created_customer', 10, 3);

function handle_created_customer($customer_id, $new_customer_data, $password_generated) {
    $fName = htmlspecialchars(strip_tags($_POST['wpuef_options']['c11']));
    $lName = htmlspecialchars(strip_tags($_POST['wpuef_options']['c15']));
    $street = htmlspecialchars(strip_tags($_POST['wpuef_options']['c23']));
    $pCode = htmlspecialchars(strip_tags($_POST['wpuef_options']['c31']));
    $city = htmlspecialchars(strip_tags($_POST['wpuef_options']['c35']));
    $phone = htmlspecialchars(strip_tags($_POST['wpuef_options']['c43']));
    sendXMLData($new_customer_data['user_email'], $fName, $lName, $phone, $city, $street, $pCode);
}

is it possible in the same file to set a new function that will check the $phone > 10 characters to show an error.

I hope you understand my problem, thank you all for help


Solution

  • You could use PHP's strlen() function to get the length of a String.
    $phoneNumberLength = strlen((string)$phone); will give you the number of characters in your phone number.