The input component is a wrapper to the HTML input element with custom styling and additional functionality. It accepts most of the same properties as the HTML input, but works great on desktop devices and integrates with the keyboard on mobile devices.
The input component is meant for text type inputs only, such as "text", "password", "email", "number", "search", "tel", and "url". It supports all standard text input events including keyup, keydown, keypress, and more. The default type is "text".
Labels will take up the width of their content by default. Developers can use the labelPlacement property to control how the label is placed relative to the control.
Inputs offer two options for clearing the input based on how you interact with it. The first way is by adding the clearInput property which will show a clear button when the input has a value. The second way is the clearOnEdit property which will clear the input after it has been blurred and then typed in again. Inputs with a type set to "password" will have clearOnEdit enabled by default.
Helper and error text can be used inside of an input with the helperText and errorText property. The error text will not be displayed unless the ion-invalid and ion-touched classes are added to the ion-input. This ensures errors are not shown before the user has a chance to enter data.
In Angular, this is done automatically through form validation. In JavaScript, React and Vue, the class needs to be manually added based on your own validation.
The input counter is text that displays under an input to notify the user of how many characters have been entered out of the total that the input will accept. When adding counter, the default behavior is to format the value that gets displayed as inputLength / maxLength. This behavior can be customized by passing in a formatter function to the counterFormatter property.
Developers can use the ionInput event to update the input value in response to user input such as a keypress. This is useful for filtering out invalid or unwanted characters.
When storing the value in a state variable, we recommend updating both the state variable and the ion-input component value. This ensures that the state variable and the ion-input component value remain in sync.
Setting the color property changes the color palette for each input. On ios mode, this property changes the caret color. On md mode, this property changes the caret color and the highlight/underline color.
Input uses scoped encapsulation, which means it will automatically scope its CSS by appending each of the styles with an additional class at runtime. Overriding scoped selectors in CSS requires a higher specificity selector. Targeting the ion-input for customization will not work; therefore we recommend adding a class and customizing it that way.
A simpler input syntax was introduced in Ionic 7.0. This new syntax reduces the boilerplate required to setup an input, resolves accessibility issues, and improves the developer experience.
Developers can perform this migration one input at a time. While developers can continue using the legacy syntax, we recommend migrating as soon as possible.
Remove ion-label and use the label property on ion-input instead. The placement of the label can be configured using the labelPlacement property on ion-input.
Move input-specific properties from ion-item on to ion-input. This includes the counter, counterFormatter, fill, and shape properties.
Remove usages of the helper and error slots on ion-item and use the helperText and errorText properties on ion-input instead.
JavaScript
Angular
React
Vue
<!-- Label and Label Position --> <!-- Before --> <ion-item> <ion-labelposition="floating">Email:</ion-label> <ion-input></ion-input> </ion-item> <!-- After --> <ion-item> <ion-inputlabel="Email:"label-placement="floating"></ion-input> </ion-item> <!-- Fill --> <!-- Before --> <ion-itemfill="outline"shape="round"> <ion-labelposition="floating">Email:</ion-label> <ion-input></ion-input> </ion-item> <!-- After --> <!-- Inputs using `fill` should not be placed in ion-item --> <ion-inputfill="outline"shape="round"label="Email:"label-placement="floating"></ion-input> <!-- Input-specific features on ion-item --> <!-- Before --> <ion-itemcounter="true"> <ion-labelposition="floating">Email:</ion-label> <ion-inputmaxlength="100"></ion-input> <divslot="helper">Enter an email</div> <divslot="error">Please enter a valid email</div> </ion-item> <!-- After --> <!-- Metadata such as counters and helper text should not be used when an input is in an item/list. If you need to provide more context on a input, consider using an ion-note underneath the ion-list. --> <ion-input label="Email:" counter="true" maxlength="100" helper-text="Enter an email" error-text="Please enter a valid email" ></ion-input>
<!-- Label and Label Position --> <!-- Before --> <ion-item> <ion-labelposition="floating">Email:</ion-label> <ion-input></ion-input> </ion-item> <!-- After --> <ion-item> <ion-inputlabel="Email:"labelPlacement="floating"></ion-input> </ion-item> <!-- Fill --> <!-- Before --> <ion-itemfill="outline"shape="round"> <ion-labelposition="floating">Email:</ion-label> <ion-input></ion-input> </ion-item> <!-- After --> <!-- Inputs using `fill` should not be placed in ion-item --> <ion-inputfill="outline"shape="round"label="Email:"labelPlacement="floating"></ion-input> <!-- Input-specific features on ion-item --> <!-- Before --> <ion-item[counter]="true"> <ion-labelposition="floating">Email:</ion-label> <ion-inputmaxlength="100"></ion-input> <divslot="helper">Enter an email</div> <divslot="error">Please enter a valid email</div> </ion-item> <!-- After --> <!-- Metadata such as counters and helper text should not be used when an input is in an item/list. If you need to provide more context on a input, consider using an ion-note underneath the ion-list. --> <ion-input label="Email:" [counter]="true" maxlength="100" helperText="Enter an email" errorText="Please enter a valid email" ></ion-input>
{/* Label and Label Position */} {/* Before */} <IonItem> <IonLabelposition="floating">Email:</IonLabel> <IonInput></IonInput> </IonItem> {/* After */} <IonItem> <IonInputlabel="Email:"labelPlacement="floating"></IonInput> </IonItem> {/* Fill */} {/* Before */} <IonItemfill="outline"shape="round"> <IonLabelposition="floating">Email:</IonLabel> <IonInput></IonInput> </IonItem> {/* After */} {/* Inputs using `fill` should not be placed in IonItem */} <IonInputfill="outline"shape="round"label="Email:"labelPlacement="floating"></IonInput> {/* Input-specific features on IonItem */} {/* Before */} <IonItemcounter={true}> <IonLabelposition="floating">Email:</IonLabel> <IonInputmaxlength="100"></IonInput> <divslot="helper">Enter an email</div> <divslot="error">Please enter a valid email</div> </IonItem> {/* After */} {/* Metadata such as counters and helper text should not be used when an input is in an item/list. If you need to provide more context on a input, consider using an IonNote underneath the IonList. */} <IonInput label="Email:" counter={true} maxlength="100" helperText="Enter an email" errorText="Please enter a valid email" ></IonInput>
<!-- Label and Label Position --> <!-- Before --> <ion-item> <ion-labelposition="floating">Email:</ion-label> <ion-input></ion-input> </ion-item> <!-- After --> <ion-item> <ion-inputlabel="Email:"label-placement="floating"></ion-input> </ion-item> <!-- Fill --> <!-- Before --> <ion-itemfill="outline"shape="round"> <ion-labelposition="floating">Email:</ion-label> <ion-input></ion-input> </ion-item> <!-- After --> <!-- Inputs using `fill` should not be placed in ion-item --> <ion-inputfill="outline"shape="round"label="Email:"label-placement="floating"></ion-input> <!-- Input-specific features on ion-item --> <!-- Before --> <ion-item:counter="true"> <ion-labelposition="floating">Email:</ion-label> <ion-inputmaxlength="100"></ion-input> <divslot="helper">Enter an email</div> <divslot="error">Please enter a valid email</div> </ion-item> <!-- After --> <!-- Metadata such as counters and helper text should not be used when an input is in an item/list. If you need to provide more context on a input, consider using an ion-note underneath the ion-list. --> <ion-input label="Email:" :counter="true" maxlength="100" helper-text="Enter an email" error-text="Please enter a valid email" ></ion-input>
Ionic uses heuristics to detect if an app is using the modern input syntax. In some instances, it may be preferable to continue using the legacy syntax. Developers can set the legacy property on ion-input to true to force that instance of the input to use the legacy syntax.
While not required, this interface can be used in place of the CustomEvent interface for stronger typing with Ionic events emitted from this component.
Indicates whether and how the text value should be automatically capitalized as it is entered/edited by the user. Available options: "off", "none", "on", "sentences", "words", "characters".
The color to use from your application's color palette. Default options are: "primary", "secondary", "tertiary", "success", "warning", "danger", "light", "medium", and "dark". For more information on colors, see theming.
If true, a character counter will display the ratio of characters used and the total character limit. Developers must also set the maxlength property for the counter to be calculated correctly.
Where to place the label relative to the input. "start": The label will appear to the left of the input in LTR and to the right in RTL. "end": The label will appear to the right of the input in LTR and to the left in RTL. "floating": The label will appear smaller and above the input when the input is focused or it has a value. Otherwise it will appear on top of the input. "stacked": The label will appear smaller and above the input regardless even when the input is blurred or has no value. "fixed": The label has the same behavior as "start" except it also has a fixed width. Long text will be truncated with ellipses ("...").
Set the legacy property to true to forcibly use the legacy form control markup. Ionic will only opt components in to the modern form markup when they are using either the aria-label attribute or the label property. As a result, the legacy property should only be used as an escape hatch when you want to avoid this automatic opt-in behavior. Note that this property will be removed in an upcoming major release of Ionic, and all form components will be opted-in to using the modern form markup.
If the value of the type attribute is text, email, search, password, tel, or url, this attribute specifies the maximum number of characters that the user can enter.
If the value of the type attribute is text, email, search, password, tel, or url, this attribute specifies the minimum number of characters that the user can enter.
A regular expression that the value is checked against. The pattern must match the entire value, not just some subset. Use the title attribute to describe the pattern to help the user. This attribute applies when the value of the type attribute is "text", "search", "tel", "url", "email", "date", or "password", otherwise it is ignored. When the type attribute is "date", pattern will only be used in browsers that do not support the "date" input type natively. See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date for more information.
Instructional text that shows before the input has a value. This property applies only when the type property is set to "email", "number", "password", "search", "tel", "text", or "url", otherwise it is ignored.
The initial size of the control. This value is in pixels unless the value of the type attribute is "text" or "password", in which case it is an integer number of characters. This attribute applies only when the type attribute is set to "text", "search", "tel", "url", "email", or "password", otherwise it is ignored.
Works with the min and max attributes to limit the increments at which a value can be set. Possible values are: "any" or a positive floating point number.
The ionChange event is fired for <ion-input> elements when the user modifies the element's value. Unlike the ionInput event, the ionChange event is not necessarily fired for each alteration to an element's value.
Depending on the way the users interacts with the element, the ionChange event fires at a different moment: - When the user commits the change explicitly (e.g. by selecting a date from a date picker for <ion-input type="date">, pressing the "Enter" key, etc.). - When the element loses focus after its value has changed: for elements where the user's interaction is typing.
ionFocus
Emitted when the input has focus.
ionInput
The ionInput event fires when the value of an <ion-input> element has been changed.
For elements that accept text input (type=text, type=tel, etc.), the interface is InputEvent; for others, the interface is Event. If the input is cleared on edit, the type is null.
The label text to associate with the input. Use the labelPlacement property to control where the label is placed relative to the input. Use this if you need to render a label with custom HTML. (EXPERIMENTAL)