How to add custom field in registration form and add jquery validation in Magento2.2.5?Remove Confirm password field in magento 2 Registration formMagento 2 : How to Add Custom Input Field in Customer Registration FormHow to create a custom field in Magento 2 user registration?Magento2 Create account form custom field server side validationCustom form field not showing validation messageMagento 2.2.3 How to change validation message Or add new custom rule in system config fieldHow to save custom frontend form to database in Magento2.2.5?Magento 1.9 How to add custom field in checkout registration formjQuery validation not working on pressing enter key in Magento2.2.5?How to remove default contact form validation in Magento2.2.5?

Was it really necessary for the Lunar Module to have 2 stages?

Why was Germany not as successful as other Europeans in establishing overseas colonies?

Examples of non trivial equivalence relations , I mean equivalence relations without the expression " same ... as" in their definition?

Please, smoke with good manners

Pawn Sacrifice Justification

Do I have an "anti-research" personality?

Why does nature favour the Laplacian?

How to create an ad-hoc wireless network in Ubuntu

When to use 1/Ka vs Kb

What is the strongest case that can be made in favour of the UK regaining some control over fishing policy after Brexit?

Tikz oriented simplex

Do I have to worry about players making “bad” choices on level up?

You look catfish vs You look like a catfish

Can a creature tell when it has been affected by a Divination wizard's Portent?

Why do computer-science majors learn calculus?

Multiple options for Pseudonyms

Python "triplet" dictionary?

Phrase for the opposite of "foolproof"

Why is current rating for multicore cable lower than single core with the same cross section?

Unexpected email from Yorkshire Bank

Given what happens in Endgame, why doesn't Dormammu come back to attack the universe?

Volunteering in England

What does YCWCYODFTRFDTY mean?

How to set the font color of quantity objects (Version 11.3 vs version 12)?



How to add custom field in registration form and add jquery validation in Magento2.2.5?


Remove Confirm password field in magento 2 Registration formMagento 2 : How to Add Custom Input Field in Customer Registration FormHow to create a custom field in Magento 2 user registration?Magento2 Create account form custom field server side validationCustom form field not showing validation messageMagento 2.2.3 How to change validation message Or add new custom rule in system config fieldHow to save custom frontend form to database in Magento2.2.5?Magento 1.9 How to add custom field in checkout registration formjQuery validation not working on pressing enter key in Magento2.2.5?How to remove default contact form validation in Magento2.2.5?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















How to add custom field in registration form and save in database and add jquery validation in Magento2.2.5?



Any help would be appreciated










share|improve this question






















  • please check the below given answers magento.stackexchange.com/a/272438/52244 if it match your expectation please mark as valid answer.so if someone else got some concern they can use this valid answer. more help will be appreciated.

    – Kanhaiya lal
    Apr 25 at 13:38

















2















How to add custom field in registration form and save in database and add jquery validation in Magento2.2.5?



Any help would be appreciated










share|improve this question






















  • please check the below given answers magento.stackexchange.com/a/272438/52244 if it match your expectation please mark as valid answer.so if someone else got some concern they can use this valid answer. more help will be appreciated.

    – Kanhaiya lal
    Apr 25 at 13:38













2












2








2








How to add custom field in registration form and save in database and add jquery validation in Magento2.2.5?



Any help would be appreciated










share|improve this question














How to add custom field in registration form and save in database and add jquery validation in Magento2.2.5?



Any help would be appreciated







magento2 jquery validation registration custom-field






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 25 at 6:52









AmyAmy

36810




36810












  • please check the below given answers magento.stackexchange.com/a/272438/52244 if it match your expectation please mark as valid answer.so if someone else got some concern they can use this valid answer. more help will be appreciated.

    – Kanhaiya lal
    Apr 25 at 13:38

















  • please check the below given answers magento.stackexchange.com/a/272438/52244 if it match your expectation please mark as valid answer.so if someone else got some concern they can use this valid answer. more help will be appreciated.

    – Kanhaiya lal
    Apr 25 at 13:38
















please check the below given answers magento.stackexchange.com/a/272438/52244 if it match your expectation please mark as valid answer.so if someone else got some concern they can use this valid answer. more help will be appreciated.

– Kanhaiya lal
Apr 25 at 13:38





please check the below given answers magento.stackexchange.com/a/272438/52244 if it match your expectation please mark as valid answer.so if someone else got some concern they can use this valid answer. more help will be appreciated.

– Kanhaiya lal
Apr 25 at 13:38










2 Answers
2






active

oldest

votes


















1














Adding a custom field in customer registration page



first if need then create a custom module follow this link



then need to create a file COMPANYCUSTOMMODULESetupInstallData.php to creating custom field



<?php

namespace COMPANYCUSTOMMODULESetup;

use MagentoCustomerModelCustomer;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;

class InstallData implements MagentoFrameworkSetupInstallDataInterface

private $eavSetupFactory;

private $eavConfig;

private $attributeResource;

/**
* InstallData constructor.
* @param MagentoEavSetupEavSetupFactory $eavSetupFactory
* @param MagentoEavModelConfig $eavConfig
* @param MagentoCustomerModelResourceModelAttribute $attributeResource
*/
public function __construct(
MagentoEavSetupEavSetupFactory $eavSetupFactory,
MagentoEavModelConfig $eavConfig,
MagentoCustomerModelResourceModelAttribute $attributeResource
)

$this->eavSetupFactory = $eavSetupFactory;
$this->eavConfig = $eavConfig;
$this->attributeResource = $attributeResource;


/**
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @throws MagentoFrameworkExceptionAlreadyExistsException
* @throws MagentoFrameworkExceptionLocalizedException
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)

$customField = "custom_field";
$customFieldLabel = "Custom Field 1";
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

$eavSetup->removeAttribute(Customer::ENTITY, $customField);

$attributeSetId = $eavSetup->getDefaultAttributeSetId(Customer::ENTITY);
$attributeGroupId = $eavSetup->getDefaultAttributeGroupId(Customer::ENTITY);

$eavSetup->addAttribute(Customer::ENTITY, $customField, [
// Attribute parameters
'type' => 'varchar',
'label' => $customFieldLabel,
'input' => 'text',
'required' => true,
'visible' => true,
'user_defined' => true,
'sort_order' => 990,
'position' => 990,
'system' => 0,
]);

$attribute = $this->eavConfig->getAttribute(Customer::ENTITY, $customField);
$attribute->setData('attribute_set_id', $attributeSetId);
$attribute->setData('attribute_group_id', $attributeGroupId);

/*
//You can use this attribute in the following forms
adminhtml_checkout
adminhtml_customer // for admin page under customer edit account info
adminhtml_customer_address
customer_account_create // for store-front registration page
customer_account_edit // for store-front after logged-in account edit page
customer_address_edit
customer_register_address
*/

$attribute->setData('used_in_forms', [
'adminhtml_customer',
'customer_account_create',
'customer_account_edit'
]);

$this->attributeResource->save($attribute);




wait it will help to just create on DB level and visible in admin-level,



Need to access same custom field in store-front



so create a layout file COMPANYCUSTOMMODULEviewfrontendlayoutcustomer_account_create.xml



<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="form.additional.info">
<block class="MagentoFrameworkViewElementTemplate" name="form_additional_info_customer"
template="COMPANY_CUSTOMMODULE::additional.phtml"/>
</referenceContainer>
</body>
</page>


and to display field in store-front in registration page,
create a phtml file COMPANYCUSTOMMODULEviewfrontendtemplatesadditional.phtml



<?php
$customFieldLabel = __("Custom Field 1");
$customField = "custom_field";
?>
<div class="field <?= $customField ?> required">
<label class="label" for="<?= $customField ?>">
<span><?= $block->escapeHtml($customFieldLabel) ?></span>
</label>
<div class="control">
<input type="text" name="<?= $customField ?>" id="<?= $customField ?>" value=""
title="<?= $block->escapeHtmlAttr($customFieldLabel) ?>"
class="input-text" data-validate="required:true">
</div>
</div>


after added these files or code



run below commands :



php bin/magento setup:upgrade


php bin/magento setup:static-content:deploy (for developer mode add -f to command)



php bin/magento cache:clear


For JS validation : In above phtml file (COMPANYCUSTOMMODULEviewfrontendtemplatesadditional.phtml), if you need to add custom validation then use Magento Validation classes to data-validate attribute.



More help for JS validation follow this link



Note : COMPANYCUSTOMMODULE is the custom module, if you need you can change with your custom module.



Hope this will help you.






share|improve this answer






























    0














    Step 1 : Add your input field



     <div class="field required">
    <label for="username" class="label"><span><?php /* @escapeNotVerified */
    echo __('Username') ?></span></label>
    <div class="control">
    <input type="text" name="username" id="username"
    maxlength="50"
    title="<?php /* @escapeNotVerified */
    echo __('Username') ?>"
    class="input-text"
    data-validate="'maxlength':50, required:true, 'validateUsername':true">
    </div>
    </div>


    Step 2: Add jQuery validation in the template.



    <script>
    requirejs([
    'jquery',
    'jquery/ui',
    'jquery/validate',
    'mage/translate',
    'mage/mage',
    ], function ($)
    $.validator.addMethod(
    'validateUsername',
    function (value)
    return value.match(/^[a-zA-Z0-9-_@.]+$/);
    ,
    $.mage.__('Enter a valid username. Valid characters are A-Z a-z 0-9 . _ - @.')
    );
    );
    </script>





    share|improve this answer























      Your Answer








      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "479"
      ;
      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: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      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%2fmagento.stackexchange.com%2fquestions%2f272374%2fhow-to-add-custom-field-in-registration-form-and-add-jquery-validation-in-magent%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Adding a custom field in customer registration page



      first if need then create a custom module follow this link



      then need to create a file COMPANYCUSTOMMODULESetupInstallData.php to creating custom field



      <?php

      namespace COMPANYCUSTOMMODULESetup;

      use MagentoCustomerModelCustomer;
      use MagentoFrameworkSetupModuleContextInterface;
      use MagentoFrameworkSetupModuleDataSetupInterface;

      class InstallData implements MagentoFrameworkSetupInstallDataInterface

      private $eavSetupFactory;

      private $eavConfig;

      private $attributeResource;

      /**
      * InstallData constructor.
      * @param MagentoEavSetupEavSetupFactory $eavSetupFactory
      * @param MagentoEavModelConfig $eavConfig
      * @param MagentoCustomerModelResourceModelAttribute $attributeResource
      */
      public function __construct(
      MagentoEavSetupEavSetupFactory $eavSetupFactory,
      MagentoEavModelConfig $eavConfig,
      MagentoCustomerModelResourceModelAttribute $attributeResource
      )

      $this->eavSetupFactory = $eavSetupFactory;
      $this->eavConfig = $eavConfig;
      $this->attributeResource = $attributeResource;


      /**
      * @param ModuleDataSetupInterface $setup
      * @param ModuleContextInterface $context
      * @throws MagentoFrameworkExceptionAlreadyExistsException
      * @throws MagentoFrameworkExceptionLocalizedException
      */
      public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)

      $customField = "custom_field";
      $customFieldLabel = "Custom Field 1";
      $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

      $eavSetup->removeAttribute(Customer::ENTITY, $customField);

      $attributeSetId = $eavSetup->getDefaultAttributeSetId(Customer::ENTITY);
      $attributeGroupId = $eavSetup->getDefaultAttributeGroupId(Customer::ENTITY);

      $eavSetup->addAttribute(Customer::ENTITY, $customField, [
      // Attribute parameters
      'type' => 'varchar',
      'label' => $customFieldLabel,
      'input' => 'text',
      'required' => true,
      'visible' => true,
      'user_defined' => true,
      'sort_order' => 990,
      'position' => 990,
      'system' => 0,
      ]);

      $attribute = $this->eavConfig->getAttribute(Customer::ENTITY, $customField);
      $attribute->setData('attribute_set_id', $attributeSetId);
      $attribute->setData('attribute_group_id', $attributeGroupId);

      /*
      //You can use this attribute in the following forms
      adminhtml_checkout
      adminhtml_customer // for admin page under customer edit account info
      adminhtml_customer_address
      customer_account_create // for store-front registration page
      customer_account_edit // for store-front after logged-in account edit page
      customer_address_edit
      customer_register_address
      */

      $attribute->setData('used_in_forms', [
      'adminhtml_customer',
      'customer_account_create',
      'customer_account_edit'
      ]);

      $this->attributeResource->save($attribute);




      wait it will help to just create on DB level and visible in admin-level,



      Need to access same custom field in store-front



      so create a layout file COMPANYCUSTOMMODULEviewfrontendlayoutcustomer_account_create.xml



      <?xml version="1.0"?>
      <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
      <body>
      <referenceContainer name="form.additional.info">
      <block class="MagentoFrameworkViewElementTemplate" name="form_additional_info_customer"
      template="COMPANY_CUSTOMMODULE::additional.phtml"/>
      </referenceContainer>
      </body>
      </page>


      and to display field in store-front in registration page,
      create a phtml file COMPANYCUSTOMMODULEviewfrontendtemplatesadditional.phtml



      <?php
      $customFieldLabel = __("Custom Field 1");
      $customField = "custom_field";
      ?>
      <div class="field <?= $customField ?> required">
      <label class="label" for="<?= $customField ?>">
      <span><?= $block->escapeHtml($customFieldLabel) ?></span>
      </label>
      <div class="control">
      <input type="text" name="<?= $customField ?>" id="<?= $customField ?>" value=""
      title="<?= $block->escapeHtmlAttr($customFieldLabel) ?>"
      class="input-text" data-validate="required:true">
      </div>
      </div>


      after added these files or code



      run below commands :



      php bin/magento setup:upgrade


      php bin/magento setup:static-content:deploy (for developer mode add -f to command)



      php bin/magento cache:clear


      For JS validation : In above phtml file (COMPANYCUSTOMMODULEviewfrontendtemplatesadditional.phtml), if you need to add custom validation then use Magento Validation classes to data-validate attribute.



      More help for JS validation follow this link



      Note : COMPANYCUSTOMMODULE is the custom module, if you need you can change with your custom module.



      Hope this will help you.






      share|improve this answer



























        1














        Adding a custom field in customer registration page



        first if need then create a custom module follow this link



        then need to create a file COMPANYCUSTOMMODULESetupInstallData.php to creating custom field



        <?php

        namespace COMPANYCUSTOMMODULESetup;

        use MagentoCustomerModelCustomer;
        use MagentoFrameworkSetupModuleContextInterface;
        use MagentoFrameworkSetupModuleDataSetupInterface;

        class InstallData implements MagentoFrameworkSetupInstallDataInterface

        private $eavSetupFactory;

        private $eavConfig;

        private $attributeResource;

        /**
        * InstallData constructor.
        * @param MagentoEavSetupEavSetupFactory $eavSetupFactory
        * @param MagentoEavModelConfig $eavConfig
        * @param MagentoCustomerModelResourceModelAttribute $attributeResource
        */
        public function __construct(
        MagentoEavSetupEavSetupFactory $eavSetupFactory,
        MagentoEavModelConfig $eavConfig,
        MagentoCustomerModelResourceModelAttribute $attributeResource
        )

        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig = $eavConfig;
        $this->attributeResource = $attributeResource;


        /**
        * @param ModuleDataSetupInterface $setup
        * @param ModuleContextInterface $context
        * @throws MagentoFrameworkExceptionAlreadyExistsException
        * @throws MagentoFrameworkExceptionLocalizedException
        */
        public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)

        $customField = "custom_field";
        $customFieldLabel = "Custom Field 1";
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $eavSetup->removeAttribute(Customer::ENTITY, $customField);

        $attributeSetId = $eavSetup->getDefaultAttributeSetId(Customer::ENTITY);
        $attributeGroupId = $eavSetup->getDefaultAttributeGroupId(Customer::ENTITY);

        $eavSetup->addAttribute(Customer::ENTITY, $customField, [
        // Attribute parameters
        'type' => 'varchar',
        'label' => $customFieldLabel,
        'input' => 'text',
        'required' => true,
        'visible' => true,
        'user_defined' => true,
        'sort_order' => 990,
        'position' => 990,
        'system' => 0,
        ]);

        $attribute = $this->eavConfig->getAttribute(Customer::ENTITY, $customField);
        $attribute->setData('attribute_set_id', $attributeSetId);
        $attribute->setData('attribute_group_id', $attributeGroupId);

        /*
        //You can use this attribute in the following forms
        adminhtml_checkout
        adminhtml_customer // for admin page under customer edit account info
        adminhtml_customer_address
        customer_account_create // for store-front registration page
        customer_account_edit // for store-front after logged-in account edit page
        customer_address_edit
        customer_register_address
        */

        $attribute->setData('used_in_forms', [
        'adminhtml_customer',
        'customer_account_create',
        'customer_account_edit'
        ]);

        $this->attributeResource->save($attribute);




        wait it will help to just create on DB level and visible in admin-level,



        Need to access same custom field in store-front



        so create a layout file COMPANYCUSTOMMODULEviewfrontendlayoutcustomer_account_create.xml



        <?xml version="1.0"?>
        <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <body>
        <referenceContainer name="form.additional.info">
        <block class="MagentoFrameworkViewElementTemplate" name="form_additional_info_customer"
        template="COMPANY_CUSTOMMODULE::additional.phtml"/>
        </referenceContainer>
        </body>
        </page>


        and to display field in store-front in registration page,
        create a phtml file COMPANYCUSTOMMODULEviewfrontendtemplatesadditional.phtml



        <?php
        $customFieldLabel = __("Custom Field 1");
        $customField = "custom_field";
        ?>
        <div class="field <?= $customField ?> required">
        <label class="label" for="<?= $customField ?>">
        <span><?= $block->escapeHtml($customFieldLabel) ?></span>
        </label>
        <div class="control">
        <input type="text" name="<?= $customField ?>" id="<?= $customField ?>" value=""
        title="<?= $block->escapeHtmlAttr($customFieldLabel) ?>"
        class="input-text" data-validate="required:true">
        </div>
        </div>


        after added these files or code



        run below commands :



        php bin/magento setup:upgrade


        php bin/magento setup:static-content:deploy (for developer mode add -f to command)



        php bin/magento cache:clear


        For JS validation : In above phtml file (COMPANYCUSTOMMODULEviewfrontendtemplatesadditional.phtml), if you need to add custom validation then use Magento Validation classes to data-validate attribute.



        More help for JS validation follow this link



        Note : COMPANYCUSTOMMODULE is the custom module, if you need you can change with your custom module.



        Hope this will help you.






        share|improve this answer

























          1












          1








          1







          Adding a custom field in customer registration page



          first if need then create a custom module follow this link



          then need to create a file COMPANYCUSTOMMODULESetupInstallData.php to creating custom field



          <?php

          namespace COMPANYCUSTOMMODULESetup;

          use MagentoCustomerModelCustomer;
          use MagentoFrameworkSetupModuleContextInterface;
          use MagentoFrameworkSetupModuleDataSetupInterface;

          class InstallData implements MagentoFrameworkSetupInstallDataInterface

          private $eavSetupFactory;

          private $eavConfig;

          private $attributeResource;

          /**
          * InstallData constructor.
          * @param MagentoEavSetupEavSetupFactory $eavSetupFactory
          * @param MagentoEavModelConfig $eavConfig
          * @param MagentoCustomerModelResourceModelAttribute $attributeResource
          */
          public function __construct(
          MagentoEavSetupEavSetupFactory $eavSetupFactory,
          MagentoEavModelConfig $eavConfig,
          MagentoCustomerModelResourceModelAttribute $attributeResource
          )

          $this->eavSetupFactory = $eavSetupFactory;
          $this->eavConfig = $eavConfig;
          $this->attributeResource = $attributeResource;


          /**
          * @param ModuleDataSetupInterface $setup
          * @param ModuleContextInterface $context
          * @throws MagentoFrameworkExceptionAlreadyExistsException
          * @throws MagentoFrameworkExceptionLocalizedException
          */
          public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)

          $customField = "custom_field";
          $customFieldLabel = "Custom Field 1";
          $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

          $eavSetup->removeAttribute(Customer::ENTITY, $customField);

          $attributeSetId = $eavSetup->getDefaultAttributeSetId(Customer::ENTITY);
          $attributeGroupId = $eavSetup->getDefaultAttributeGroupId(Customer::ENTITY);

          $eavSetup->addAttribute(Customer::ENTITY, $customField, [
          // Attribute parameters
          'type' => 'varchar',
          'label' => $customFieldLabel,
          'input' => 'text',
          'required' => true,
          'visible' => true,
          'user_defined' => true,
          'sort_order' => 990,
          'position' => 990,
          'system' => 0,
          ]);

          $attribute = $this->eavConfig->getAttribute(Customer::ENTITY, $customField);
          $attribute->setData('attribute_set_id', $attributeSetId);
          $attribute->setData('attribute_group_id', $attributeGroupId);

          /*
          //You can use this attribute in the following forms
          adminhtml_checkout
          adminhtml_customer // for admin page under customer edit account info
          adminhtml_customer_address
          customer_account_create // for store-front registration page
          customer_account_edit // for store-front after logged-in account edit page
          customer_address_edit
          customer_register_address
          */

          $attribute->setData('used_in_forms', [
          'adminhtml_customer',
          'customer_account_create',
          'customer_account_edit'
          ]);

          $this->attributeResource->save($attribute);




          wait it will help to just create on DB level and visible in admin-level,



          Need to access same custom field in store-front



          so create a layout file COMPANYCUSTOMMODULEviewfrontendlayoutcustomer_account_create.xml



          <?xml version="1.0"?>
          <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
          <body>
          <referenceContainer name="form.additional.info">
          <block class="MagentoFrameworkViewElementTemplate" name="form_additional_info_customer"
          template="COMPANY_CUSTOMMODULE::additional.phtml"/>
          </referenceContainer>
          </body>
          </page>


          and to display field in store-front in registration page,
          create a phtml file COMPANYCUSTOMMODULEviewfrontendtemplatesadditional.phtml



          <?php
          $customFieldLabel = __("Custom Field 1");
          $customField = "custom_field";
          ?>
          <div class="field <?= $customField ?> required">
          <label class="label" for="<?= $customField ?>">
          <span><?= $block->escapeHtml($customFieldLabel) ?></span>
          </label>
          <div class="control">
          <input type="text" name="<?= $customField ?>" id="<?= $customField ?>" value=""
          title="<?= $block->escapeHtmlAttr($customFieldLabel) ?>"
          class="input-text" data-validate="required:true">
          </div>
          </div>


          after added these files or code



          run below commands :



          php bin/magento setup:upgrade


          php bin/magento setup:static-content:deploy (for developer mode add -f to command)



          php bin/magento cache:clear


          For JS validation : In above phtml file (COMPANYCUSTOMMODULEviewfrontendtemplatesadditional.phtml), if you need to add custom validation then use Magento Validation classes to data-validate attribute.



          More help for JS validation follow this link



          Note : COMPANYCUSTOMMODULE is the custom module, if you need you can change with your custom module.



          Hope this will help you.






          share|improve this answer













          Adding a custom field in customer registration page



          first if need then create a custom module follow this link



          then need to create a file COMPANYCUSTOMMODULESetupInstallData.php to creating custom field



          <?php

          namespace COMPANYCUSTOMMODULESetup;

          use MagentoCustomerModelCustomer;
          use MagentoFrameworkSetupModuleContextInterface;
          use MagentoFrameworkSetupModuleDataSetupInterface;

          class InstallData implements MagentoFrameworkSetupInstallDataInterface

          private $eavSetupFactory;

          private $eavConfig;

          private $attributeResource;

          /**
          * InstallData constructor.
          * @param MagentoEavSetupEavSetupFactory $eavSetupFactory
          * @param MagentoEavModelConfig $eavConfig
          * @param MagentoCustomerModelResourceModelAttribute $attributeResource
          */
          public function __construct(
          MagentoEavSetupEavSetupFactory $eavSetupFactory,
          MagentoEavModelConfig $eavConfig,
          MagentoCustomerModelResourceModelAttribute $attributeResource
          )

          $this->eavSetupFactory = $eavSetupFactory;
          $this->eavConfig = $eavConfig;
          $this->attributeResource = $attributeResource;


          /**
          * @param ModuleDataSetupInterface $setup
          * @param ModuleContextInterface $context
          * @throws MagentoFrameworkExceptionAlreadyExistsException
          * @throws MagentoFrameworkExceptionLocalizedException
          */
          public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)

          $customField = "custom_field";
          $customFieldLabel = "Custom Field 1";
          $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

          $eavSetup->removeAttribute(Customer::ENTITY, $customField);

          $attributeSetId = $eavSetup->getDefaultAttributeSetId(Customer::ENTITY);
          $attributeGroupId = $eavSetup->getDefaultAttributeGroupId(Customer::ENTITY);

          $eavSetup->addAttribute(Customer::ENTITY, $customField, [
          // Attribute parameters
          'type' => 'varchar',
          'label' => $customFieldLabel,
          'input' => 'text',
          'required' => true,
          'visible' => true,
          'user_defined' => true,
          'sort_order' => 990,
          'position' => 990,
          'system' => 0,
          ]);

          $attribute = $this->eavConfig->getAttribute(Customer::ENTITY, $customField);
          $attribute->setData('attribute_set_id', $attributeSetId);
          $attribute->setData('attribute_group_id', $attributeGroupId);

          /*
          //You can use this attribute in the following forms
          adminhtml_checkout
          adminhtml_customer // for admin page under customer edit account info
          adminhtml_customer_address
          customer_account_create // for store-front registration page
          customer_account_edit // for store-front after logged-in account edit page
          customer_address_edit
          customer_register_address
          */

          $attribute->setData('used_in_forms', [
          'adminhtml_customer',
          'customer_account_create',
          'customer_account_edit'
          ]);

          $this->attributeResource->save($attribute);




          wait it will help to just create on DB level and visible in admin-level,



          Need to access same custom field in store-front



          so create a layout file COMPANYCUSTOMMODULEviewfrontendlayoutcustomer_account_create.xml



          <?xml version="1.0"?>
          <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
          <body>
          <referenceContainer name="form.additional.info">
          <block class="MagentoFrameworkViewElementTemplate" name="form_additional_info_customer"
          template="COMPANY_CUSTOMMODULE::additional.phtml"/>
          </referenceContainer>
          </body>
          </page>


          and to display field in store-front in registration page,
          create a phtml file COMPANYCUSTOMMODULEviewfrontendtemplatesadditional.phtml



          <?php
          $customFieldLabel = __("Custom Field 1");
          $customField = "custom_field";
          ?>
          <div class="field <?= $customField ?> required">
          <label class="label" for="<?= $customField ?>">
          <span><?= $block->escapeHtml($customFieldLabel) ?></span>
          </label>
          <div class="control">
          <input type="text" name="<?= $customField ?>" id="<?= $customField ?>" value=""
          title="<?= $block->escapeHtmlAttr($customFieldLabel) ?>"
          class="input-text" data-validate="required:true">
          </div>
          </div>


          after added these files or code



          run below commands :



          php bin/magento setup:upgrade


          php bin/magento setup:static-content:deploy (for developer mode add -f to command)



          php bin/magento cache:clear


          For JS validation : In above phtml file (COMPANYCUSTOMMODULEviewfrontendtemplatesadditional.phtml), if you need to add custom validation then use Magento Validation classes to data-validate attribute.



          More help for JS validation follow this link



          Note : COMPANYCUSTOMMODULE is the custom module, if you need you can change with your custom module.



          Hope this will help you.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 25 at 12:18









          Kanhaiya lalKanhaiya lal

          910418




          910418























              0














              Step 1 : Add your input field



               <div class="field required">
              <label for="username" class="label"><span><?php /* @escapeNotVerified */
              echo __('Username') ?></span></label>
              <div class="control">
              <input type="text" name="username" id="username"
              maxlength="50"
              title="<?php /* @escapeNotVerified */
              echo __('Username') ?>"
              class="input-text"
              data-validate="'maxlength':50, required:true, 'validateUsername':true">
              </div>
              </div>


              Step 2: Add jQuery validation in the template.



              <script>
              requirejs([
              'jquery',
              'jquery/ui',
              'jquery/validate',
              'mage/translate',
              'mage/mage',
              ], function ($)
              $.validator.addMethod(
              'validateUsername',
              function (value)
              return value.match(/^[a-zA-Z0-9-_@.]+$/);
              ,
              $.mage.__('Enter a valid username. Valid characters are A-Z a-z 0-9 . _ - @.')
              );
              );
              </script>





              share|improve this answer



























                0














                Step 1 : Add your input field



                 <div class="field required">
                <label for="username" class="label"><span><?php /* @escapeNotVerified */
                echo __('Username') ?></span></label>
                <div class="control">
                <input type="text" name="username" id="username"
                maxlength="50"
                title="<?php /* @escapeNotVerified */
                echo __('Username') ?>"
                class="input-text"
                data-validate="'maxlength':50, required:true, 'validateUsername':true">
                </div>
                </div>


                Step 2: Add jQuery validation in the template.



                <script>
                requirejs([
                'jquery',
                'jquery/ui',
                'jquery/validate',
                'mage/translate',
                'mage/mage',
                ], function ($)
                $.validator.addMethod(
                'validateUsername',
                function (value)
                return value.match(/^[a-zA-Z0-9-_@.]+$/);
                ,
                $.mage.__('Enter a valid username. Valid characters are A-Z a-z 0-9 . _ - @.')
                );
                );
                </script>





                share|improve this answer

























                  0












                  0








                  0







                  Step 1 : Add your input field



                   <div class="field required">
                  <label for="username" class="label"><span><?php /* @escapeNotVerified */
                  echo __('Username') ?></span></label>
                  <div class="control">
                  <input type="text" name="username" id="username"
                  maxlength="50"
                  title="<?php /* @escapeNotVerified */
                  echo __('Username') ?>"
                  class="input-text"
                  data-validate="'maxlength':50, required:true, 'validateUsername':true">
                  </div>
                  </div>


                  Step 2: Add jQuery validation in the template.



                  <script>
                  requirejs([
                  'jquery',
                  'jquery/ui',
                  'jquery/validate',
                  'mage/translate',
                  'mage/mage',
                  ], function ($)
                  $.validator.addMethod(
                  'validateUsername',
                  function (value)
                  return value.match(/^[a-zA-Z0-9-_@.]+$/);
                  ,
                  $.mage.__('Enter a valid username. Valid characters are A-Z a-z 0-9 . _ - @.')
                  );
                  );
                  </script>





                  share|improve this answer













                  Step 1 : Add your input field



                   <div class="field required">
                  <label for="username" class="label"><span><?php /* @escapeNotVerified */
                  echo __('Username') ?></span></label>
                  <div class="control">
                  <input type="text" name="username" id="username"
                  maxlength="50"
                  title="<?php /* @escapeNotVerified */
                  echo __('Username') ?>"
                  class="input-text"
                  data-validate="'maxlength':50, required:true, 'validateUsername':true">
                  </div>
                  </div>


                  Step 2: Add jQuery validation in the template.



                  <script>
                  requirejs([
                  'jquery',
                  'jquery/ui',
                  'jquery/validate',
                  'mage/translate',
                  'mage/mage',
                  ], function ($)
                  $.validator.addMethod(
                  'validateUsername',
                  function (value)
                  return value.match(/^[a-zA-Z0-9-_@.]+$/);
                  ,
                  $.mage.__('Enter a valid username. Valid characters are A-Z a-z 0-9 . _ - @.')
                  );
                  );
                  </script>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 25 at 7:05









                  Sudhanshu BajajSudhanshu Bajaj

                  556




                  556



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Magento Stack Exchange!


                      • 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%2fmagento.stackexchange.com%2fquestions%2f272374%2fhow-to-add-custom-field-in-registration-form-and-add-jquery-validation-in-magent%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

                      Sum ergo cogito? 1 nng

                      三茅街道4182Guuntc Dn precexpngmageondP