2

How can we limit the character of first name and last name of customer. For example I want only 30 characters are valid to enter in first & last name. As of now there is no limit on character we can add as many character we want.

Looking for advices Thanks

7
  • do you want to limit 30 character individually for first name and last name OR in combined name (first name + last name) ? Commented Oct 12, 2018 at 13:14
  • yes individually
    – Daniel_12
    Commented Oct 12, 2018 at 13:14
  • for individual , you can use maxlength property of text field of first name and last name like this link Commented Oct 12, 2018 at 13:16
  • no but how can we do this in magento 2. I am aware of maxlength="10" feature
    – Daniel_12
    Commented Oct 12, 2018 at 13:17
  • you need to override template file vendor/magento/module-customer/view/frontend/templates/widget/name.phtml and need to put maxlength property there. Commented Oct 12, 2018 at 13:23

4 Answers 4

1

Try This Code

<input type="text" name="first name" id="first_name"
       class="admin__control-text required-entry validate-alphanum-with-spaces validate-length maximum-length-30"
       maxlength="30" />

whatever you want set input field length,set in maxlength attribute

1
  • Just tried this and it didn't stop the spam accounts.
    – Krista K
    Commented Jan 18, 2023 at 16:42
1

You need to override core template file vendor/magento/module-customer/view/frontend/templates/widget/name.phtml in your theme folder using this link -

How to override .phtml files in Magento 2

and need to put maxlength property in input text field of first name and last name as suggested here.

https://www.w3schools.com/tags/att_input_maxlength.asp

hope this helps.

0
1

Vendor/Module/view/frontend/layout/checkout_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="checkout" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="checkout.root">
            <arguments>
                <argument name="jsLayout" xsi:type="array">
                    <item name="components" xsi:type="array">
                        <item name="checkout" xsi:type="array">
                            <item name="children" xsi:type="array">
                                <item name="steps" xsi:type="array">
                                    <item name="children" xsi:type="array">
                                        <item name="shipping-step" xsi:type="array">
                                            <item name="children" xsi:type="array">
                                                <item name="shippingAddress" xsi:type="array">
                                                    <item name="children" xsi:type="array">
                                                        <item name="shipping-address-fieldset" xsi:type="array">
                                                            <item name="children" xsi:type="array">
                                                                <item name="firstname" xsi:type="array">
                                                                    <item name="validation" xsi:type="array">
                                                                        <item name="min_text_length" xsi:type="number">1</item>
                                                                        <item name="max_text_length" xsi:type="number">30</item>
                                                                        <item name="required-entry" xsi:type="boolean">true</item>
                                                                        <item name="max-words" xsi:type="number">4</item>
                                                                    </item>
                                                                </item>
                                                                <item name="lastname" xsi:type="array">
                                                                    <item name="validation" xsi:type="array">
                                                                        <item name="min_text_length" xsi:type="number">1</item>
                                                                        <item name="max_text_length" xsi:type="number">30</item>
                                                                        <item name="required-entry" xsi:type="boolean">true</item>
                                                                        <item name="max-words" xsi:type="number">4</item>
                                                                    </item>
                                                                </item>
                                                            </item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </item>
                </argument>
            </arguments>
        </referenceBlock>
    </body>
</page>
1
  • Working on Magento 2.4.3 Tested and verified.Upvoted Commented Jan 12, 2023 at 9:32
0
  1. You should override vendor/magento/module-customer/view/frontend/templates/widget/name.phtml in your theme. Then you can add classes "validate-length maximum-length-30" to those inputs.

  2. You should also override it in checkout. You need to find information how to modify validation of checkout fields before render them. You can do it e.g by plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.