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.
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.
1import{LightningElement}from "lwc";23export 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.
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.
Parameter
Type
Description
salutation
string
The value of the salutation field.
firstName
string
The value of the first name field.
middleName
string
The value of the middle name field.
lastName
string
The value of the last name field.
informalName
string
The value of the informal name field.
suffix
string
The value of the suffix field.
validity
object
The validity state of the element.
The change event properties are as follows.
Property
Value
Description
bubbles
true
This event bubbles up through the DOM.
cancelable
false
This event has no default behavior that can be canceled. You can’t call preventDefault() on this event.
composed
true
This event propagates outside of the component in which it was dispatched.
If present, the input name field is disabled and users cannot interact with it.
boolean
false
field-level-help
Help text detailing the purpose and function of the input.
string
fields-to-display
List of fields to be displayed on the component. This value defaults to ['firstName', 'salutation', 'lastName']. Other field values include middleName, informalName, suffix.
list
first-name
Displays the First Name field.
string
first-name-label
Reserved for internal use.
informal-name
Displays the Informal Name field.
string
informal-name-label
Reserved for internal use.
label
The label of the input name field.
string
last-name
Displays the Last Name field.
string
last-name-label
Reserved for internal use.
locale
Specifies the locale used to determine the layout of the name fields. This value defaults to en-US.
string
en-US
middle-name
Displays the Middle Name field.
string
middle-name-label
Reserved for internal use.
options
Displays a list of salutation options, such as Dr. or Mrs., provided as label-value pairs.
list
read-only
If present, the input name field is read-only and cannot be edited.
boolean
false
required
If 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.
boolean
false
salutation
Displays the Salutation field as a dropdown menu. Use the options attribute to provide salutations in an array of label-value pairs.
string
salutation-label
Reserved for internal use.
suffix
Displays the Suffix field.
string
suffix-label
Reserved for internal use.
validity
Represents the validity states that an element can be in, with respect to constraint validation.
object
variant
The 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.
string
standard
Methods
Name
Description
Argument Name
Argument Type
Argument Description
blur
Removes keyboard focus from the input element.
checkValidity
Returns the valid property value (Boolean) on the ValidityState object to indicate whether input name fields have validity errors.
focus
Sets focus on the first input field.
reportValidity
Displays the error messages and returns false if the input is invalid. If the input is valid, reportValidity() clears displayed error messages and returns true.
setCustomValidityForField
Sets a custom error message to be displayed for the input name fields when the input value is submitted.
message
string
The string that describes the error. If message is an empty string, the error message is reset.
fieldName
string
The name of the input name field.
showHelpMessageIfInvalid
Displays error messages on the input fields if the entries are invalid.