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

Daniel_12
  • 680
  • 11
  • 28

4 Answers4

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

Balwant Singh
  • 1,270
  • 2
  • 12
  • 28
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.

Shashank Kumrawat
  • 2,110
  • 23
  • 61
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>
akashmalik
  • 486
  • 3
  • 10
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.

K. Maliszewski
  • 737
  • 5
  • 14