Input Name

lightning-input-name

Represents a name compound field.

For Use In

Lightning Experience, Experience Builder Sites, Salesforce Mobile App, Lightning Out (Beta), Standalone Lightning App

A lightning-input-name component is a name compound field represented by HTML input elements of type text. The Salutation field is a dropdown menu that accepts an array of label-value pairs.

By default, lightning-input-name displays Salutation, First Name, and Last Name fields. Use the fields-to-display attribute to specify a different list of fields to display. The component supports these field names for fields-to-display.

  • firstName
  • lastName
  • middleName
  • informalName
  • suffix
  • salutation

Design 

lightning-input-name implements the form element blueprint in the Salesforce Lightning Design System (SLDS). The input fields adapt to SLDS 1 or SLDS 2 styling based on the org’s theme or the container app that you use.

SLDS 1SLDS 2
DesignForm ElementForm Element
For Use InLightning Experience, Experience Builder sites, Salesforce mobile app, Lightning Out (Beta), Mobile OfflineLightning Experience

Specify Field Names 

To provide initial values for fields, specify the field names as attributes in the component by using the dash-separated format of the field names. For example, specify first-name instead of firstName. Use the options attribute to specify the values to display in the Salutation dropdown menu.

This example creates a simple input name, consisting of just the First Name and Last Name fields, without specifying initial values. The rendered fields display default placeholder text.

1<template>
2    <div>
3        <lightning-input-name label="My Name" fields-to-display={fields}>
4        </lightning-input-name>
5    </div>
6</template>

JavaScript file:

1import { LightningElement } from "lwc";
2
3export default class InputName extends LightningElement {
4  fieldList = ["firstName", "lastName"];
5  get fields() {
6    return this.fieldList;
7  }
8}

This example creates an input name that specifies values for first name, middle name, last name, informal name, suffix. The Salutation dropdown menu is set to display “Mr.” by default. The fields-to-display attribute determines which fields are rendered. Although all possible fields are specified inside the component, only the First Name and Last Name display.

1<template>
2    <div>
3        <lightning-input-name
4            label="Contact Name"
5            first-name="John"
6            middle-name="Middleton"
7            last-name="Doe"
8            informal-name="Jo"
9            suffix="The 3rd"
10            salutation="Mr."
11            options={salutationOptions}
12            fields-to-display={fields}
13        >
14        </lightning-input-name>
15    </div>
16</template>

JavaScript file:

1import { LightningElement } from "lwc";
2
3export default class InputName extends LightningElement {
4  salutationsList = [
5    { label: "Mr.", value: "Mr." },
6    { label: "Ms.", value: "Ms." },
7    { label: "Mrs.", value: "Mrs." },
8    { label: "Dr.", value: "Dr." },
9    { label: "Prof.", value: "Prof." },
10  ];
11
12  get salutationOptions() {
13    return this.salutationsList;
14  }
15
16  fieldList = ["firstName", "lastName"];
17  get fields() {
18    return this.fieldList;
19  }
20}

lightning-input-name uses the onchange event handler to listen to a change to its field values.

1<p>Your first name: {firstname}</p>
2<lightning-input-name
3    label="Name"
4    first-name={firstname}
5    middle-name="Middleton"
6    last-name="Doe"
7    options={salutationOptions}
8    onchange={handleChange}></lightning-input-name>

To bind the input value on the name fields, use the event.target property.

1import { LightningElement } from "lwc";
2
3export default class InputNameBase extends LightningElement {
4  firstname = "John";
5  salutationsList = [
6    { label: "Mr.", value: "Mr." },
7    { label: "Ms.", value: "Ms." },
8    { label: "Mrs.", value: "Mrs." },
9    { label: "Dr.", value: "Dr." },
10    { label: "Prof.", value: "Prof." },
11  ];
12
13  get salutationOptions() {
14    return this.salutationsList;
15  }
16
17  handleChange(event) {
18    this.firstname = event.target.firstName;
19  }
20}

See the Custom Events section for a list of event.target properties. For more information, see Data Binding in a Template.

Use the Locale Information 

In Lightning Experience, the locale value corresponds to the Locale field on the Language & Time Zone page in the user’s personal settings.

By default, your org’s locale setting determines the order of the name fields.

For example, if you select “Japanese (Japan)” in the Locale field, lightning-input-name uses ja-JP as the locale.

To override the locale on your user’s settings, provide your own locale value. Specify any locale code from the list of Supported Number, Name, and Address Formats (ICU).

1<lightning-input-name
2    label="Contact Name"
3    first-name="John"
4    last-name="Doe"
5    salutation="Mr."
6    locale="fr-FR"></lightning-input-name>

If you don’t specify the locale attribute, lightning-input-name defaults to the user’s locale setting in the org.

1<h2>User's locale: {userLocale}</h2>
2<!--No locale specified -->
3<lightning-input-name
4    lwc:ref="name"
5    label="Contact Name"
6    first-name="John"
7    middle-name="Middleton"
8    last-name="Doe"
9    informal-name="Jo"
10    suffix="The 3rd"
11    salutation="Mr."
12    fields-to-display={fields}></lightning-input-name>

In this example, the userLocale property returns ja-JP based on the org user’s settings.

1import { LightningElement } from "lwc";
2
3export default class LocaleExample extends LightningElement {
4  userLocale = "";
5
6  fields = ["firstName", "lastName", "middleName", "salutation", "suffix", "informalName"];
7
8  renderedCallback() {
9    this.userLocale = this.refs.name.locale;
10  }
11}

If you pass in an invalid locale, the component uses en-US. The locale supports both hyphens and underscores, for example, en-US or en_US.

Usage Considerations 

You can use custom labels that display translated values. For more information, see the Access Static Resources, Labels, Internationalization Properties, and User IDs.

This component uses button elements for dropdown menus to comply with the Lightning Design System combobox blueprint for select-only comboboxes.

Input Validation 

When you set required, a red asterisk is displayed on the Last Name field to indicate that it’s required. An error message is displayed below the Last Name field if a user interacted with it and left it blank. The required attribute is not enforced and you must validate it before submitting a form that contains a name compound field.

To check the validity states of an input, use the validity attribute, which is based on the ValidityState object of the Constraint Validation API. You can access the validity states in your JavaScript. This validity attribute returns an object with boolean properties. For more information, see the lightning-input documentation.

Let’s say you have a lightning-button component that calls the handleClick method. You can display the error message when a user clicks the button without providing a value for the Last Name field.

1handleClick: () => {
2  var name = this.template.querySelector("lightning-input-name");
3  var isValid = name.checkValidity();
4  if (isValid) {
5    alert("Creating new contact for " + this.name);
6  } else {
7    name.showHelpMessageIfInvalid();
8  }
9};

You can override the default message by providing your own value for messageWhenValueMissing.

To programmatically display error messages on invalid fields, use the reportValidity() method. For custom validity error messages, display the message using setCustomValidityForField() and reportValidity(). For more information, see the lightning-input documentation.

Custom Events 

change

The event fired when an item is changed in the lightning-input-name component.

The change event returns the following parameters.

ParameterTypeDescription
salutationstringThe value of the salutation field.
firstNamestringThe value of the first name field.
middleNamestringThe value of the middle name field.
lastNamestringThe value of the last name field.
informalNamestringThe value of the informal name field.
suffixstringThe value of the suffix field.
validityobjectThe validity state of the element.

The change event properties are as follows.

PropertyValueDescription
bubblestrueThis event bubbles up through the DOM.
cancelablefalseThis event has no default behavior that can be canceled. You can’t call preventDefault() on this event.
composedtrueThis event propagates outside of the component in which it was dispatched.

See Also 

Use Wire Service with Base Components

Object Reference for the Salesforce Platform: Field Types

Attributes 

NameDescriptionTypeDefaultRequired
disabledIf present, the input name field is disabled and users cannot interact with it.booleanfalse
field-level-helpHelp text detailing the purpose and function of the input.string
fields-to-displayList of fields to be displayed on the component. This value defaults to ['firstName', 'salutation', 'lastName']. Other field values include middleName, informalName, suffix.list
first-nameDisplays the First Name field.string
first-name-labelReserved for internal use.
informal-nameDisplays the Informal Name field.string
informal-name-labelReserved for internal use.
labelThe label of the input name field.string
last-nameDisplays the Last Name field.string
last-name-labelReserved for internal use.
localeSpecifies the locale used to determine the layout of the name fields. This value defaults to en-US.stringen-US
middle-nameDisplays the Middle Name field.string
middle-name-labelReserved for internal use.
optionsDisplays a list of salutation options, such as Dr. or Mrs., provided as label-value pairs.list
read-onlyIf present, the input name field is read-only and cannot be edited.booleanfalse
requiredIf present, the input name field must be filled out before the form is submitted. A red asterisk is displayed on the Last Name field. An error message is displayed if a user interacts with the Last Name field and does not provide a value.booleanfalse
salutationDisplays the Salutation field as a dropdown menu. Use the options attribute to provide salutations in an array of label-value pairs.string
salutation-labelReserved for internal use.
suffixDisplays the Suffix field.string
suffix-labelReserved for internal use.
validityRepresents the validity states that an element can be in, with respect to constraint validation.object
variantThe variant changes the appearance of a name compound field. Accepted variants include standard, label-hidden, label-inline, and label-stacked. This value defaults to standard. Use label-hidden to hide the label but make it available to assistive technology. Use label-inline to horizontally align the label and name fields. Use label-stacked to place the label above the name fields.stringstandard

Methods 

NameDescriptionArgument NameArgument TypeArgument Description
blurRemoves keyboard focus from the input element.
checkValidityReturns the valid property value (Boolean) on the ValidityState object to indicate whether input name fields have validity errors.
focusSets focus on the first input field.
reportValidityDisplays the error messages and returns false if the input is invalid. If the input is valid, reportValidity() clears displayed error messages and returns true.
setCustomValidityForFieldSets a custom error message to be displayed for the input name fields when the input value is submitted.messagestringThe string that describes the error. If message is an empty string, the error message is reset.
fieldNamestringThe name of the input name field.
showHelpMessageIfInvalidDisplays error messages on the input fields if the entries are invalid.