You need to create a module and here is installData.php
namespace Custom\Module\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
/**
* Customer setup factory
*
* @var \Magento\Customer\Setup\CustomerSetupFactory
*/
private $customerSetupFactory;
/**
* Init
*
* @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
*/
public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
{
$this->customerSetupFactory = $customerSetupFactory;
}
/**
* Installs DB schema for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$entityTypeId = $customerSetup->getEntityTypeId(\Magento\Customer\Model\Customer::ENTITY);
$customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, "custom_field");
$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, "custom_field", array(
"type" => "varchar",
"backend" => "",
"label" => "Custom Field",
"input" => "text",
"source" => "",
"visible" => true,
"required" => false,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => ""
));
$custom_field = $customerSetup->getAttribute(\Magento\Customer\Model\Customer::ENTITY, "custom_field");
$custom_field = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'custom_field');
$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
$custom_field->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 1002);
$custom_field->save();
$installer->endSetup();
}
}
Note : If you apply this script in existing module than, First you need to remove your module from Setup table from DB than run below commands to make it work.
setup:upgrade
setup:static-content:deploy
cache:clean
To check if an enter key is pressed on page, bind the keypress()
to the jQuery $(document)
.
$(document).keypress(function(event){
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '13'){
alert('You pressed a "enter" key in somewhere');
}
});
to check if the submit button is pressed
$("#myButton").click(function() {
alert("Click code executed.");
});
Best 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
COMPANY\CUSTOMMODULE\Setup\InstallData.php
to creating custom fieldwait 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
COMPANY\CUSTOMMODULE\view\frontend\layout\customer_account_create.xml
and to display field in store-front in registration page, create a phtml file
COMPANY\CUSTOMMODULE\view\frontend\templates\additional.phtml
after added these files or code
run below commands :
php bin/magento setup:static-content:deploy
(for developer mode add -f to command)For JS validation : In above phtml file (
COMPANY\CUSTOMMODULE\view\frontend\templates\additional.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 :
COMPANY\CUSTOMMODULE
is the custom module, if you need you can change with your custom module.Hope this will help you.