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

To provide initial values for fields, specify the field names as attributes in the component, 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.

JavaScript file:

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.

JavaScript file:

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

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

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

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).

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

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

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.

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.

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.

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.

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.

Use Wire Service with Base Components