How to use validation input Ionic7


Create a new app ionic

1. Install Node.js and NPM: Before you start creating your Ionic app, you need to install Node.js and NPM (Node Package Manager) on your computer. You can download and install them from the official website: .

2. Install Ionic CLI: Once you have Node.js and NPM installed, you can install the Ionic CLI by running the following command in your terminal or command prompt:


npm install -g @ionic/cli

This command will install the latest version of the Ionic CLI globally on your system.
1. Create a new Ionic app: To create a new Ionic app, run the following command in your terminal or command prompt:


ionic start myApp

To create a new app with a specific template, you can use the --type option followed by the template name. For example, to create a new app with the blank template, run the following command:

ionic start myApp --type=blank

1. Run the app: Once you have created your app, you can run it in the browser by running the following command:

cd myApp ionic serve

This will start a local development server and open your app in the default web browser. You can make changes to your app and see the changes live in the browser.
That's it! You have now created a new Ionic app and are ready to start building your own mobile app.

ion-input

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.

Ionic's ion-input component is a versatile and flexible input field that allows users to enter text, numbers, or other data. Here are the basic steps for using ion-input in your Ionic application:

1. Import the ion-input component in your component's module file:

We need to create class

Define your class: Start by defining a class using the class keyword, followed by the class name. Inside the class, you can define properties and methods just like you would in JavaScript.

home.page.ts

import { Component } from '@angular/core'; export class User { text!: string; number!: number; password!: any; email!: string; tel!: any; } @Component({ selector: 'app-home', templateUrl: 'home.page.html', styleUrls: ['home.page.scss'], }) export class HomePage { showProfile: boolean = false; forms: User = new User(); constructor() { } onSubmit() { console.log(this.forms); this.showProfile = true; } onInputChange() { console.log('Data chanhed'); } }

In Angular, you can use validation on input text fields and check if they have been touched and are invalid by using the built-in properties of the form controls.

Here is an example of how to use validation on an input text field and check if it has been touched and is invalid:

In this example, we are using the "ngForm" directive to create a form and the "ngModel" directive to bind the input field to a model variable "user.username". We are also using the "required" and "minlength" validators to ensure that the input field is not empty and has at least 3 characters.

To check if the input field has been touched and is invalid, we are using the "touched" and "invalid" properties of the form control. The "touched" property is a boolean that indicates whether the input field has been touched or not, and the "invalid" property is a boolean that indicates whether the input field is invalid or not.

We are also using the "ngIf" directive to conditionally display error messages if the input field is touched and invalid. The "ngIf" directive evaluates the expression and only displays the element if the expression is true.

Inside the error message block, we are using the "errors" property of the form control to get the error messages. The "errors" property is an object that contains the errors of the form control, and we are using the keys of the object to display the appropriate error message.

1. In your HTML template, use the ion-input tag to create an input field:

2. Here, we've set the type attribute to "text" to create a simple text input field. We've also used ngModel to bind the input value to a variable called myValue.

Customize the input field by adding additional attributes or directives:

3. In this example, we've set the type attribute to "number" to create a numeric input field. We've also added the min and max attributes to set the minimum and maximum allowed values, and the debounce attribute to debounce the input changes for half a second before updating the model.

4. Use events and methods to interact with the input field:

home.page.html

<ion-header [translucent]="true"> <ion-toolbar color="secondary"> <ion-title> ion-input </ion-title> </ion-toolbar> </ion-header> <ion-content [fullscreen>]="true>"> <div *ngIf="showProfile==false"> <ion-card> <ion-card-title>Register Form</ion-card-title> <ion-card-content> <ion-list> <ion-list> <ion-item> <ion-input label="Text input" [(ngModel)]="forms.text" #text="ngModel" required placeholder="Enter text" (ionChange)="onInputChange()"> </ion-input> <div *ngIf="text.invalid && (text.dirty || text.touched)" style="color: red;"> <div *ngIf="text.errors?.['required']"> Name is required </div> <div *ngIf="text.errors?.['minlength']"> Name must be at least 4 characters long. </div> <div *ngIf="text.errors?.['forbiddenName']"> The name cannot be other than letters and numbers </div> </div> </ion-item> <ion-item> <ion-input label="Number input" type="number" [(ngModel)]="forms.number" #number="ngModel" required placeholder="000"></ion-input> <div *ngIf="number.invalid && (number.dirty || number.touched)" style="color: red;"> <div *ngIf="number.errors?.['required']"> Number is required </div> <div *ngIf="number.errors?.['minlength']"> Number must be at least 4 characters long. </div> <div *ngIf="number.errors?.['forbiddenName']"> Enter number </div> </div> </ion-item> <ion-item> <ion-input label="Password input" type="password" [(ngModel)]="forms.password" #password="ngModel" required value="password"></ion-input> <div *ngIf="password.invalid && (password.dirty || password.touched)" style="color: red;"> <div *ngIf="password.errors?.['required']"> Password is required </div> <div *ngIf="password.errors?.['minlength']"> Password must be at least 4 characters long. </div> <div *ngIf="password.errors?.['forbiddenName']"> Enter password </div> </div> </ion-item> <ion-item> <ion-input label="Email input" type="email" [(ngModel)]="forms.email" #email="ngModel" required placeholder="email@domain.com"></ion-input> <div *ngIf="email.invalid && (email.dirty || email.touched)" style="color: red;"> <div *ngIf="email.errors?.['required']"> Email is required </div> <div *ngIf="email.errors?.['forbiddenName']"> Enter email </div> </div> </ion-item> <ion-item> <ion-input label="Telephone input" type="tel" [(ngModel)]="forms.tel" #tel="ngModel" required placeholder="888-888-8888"></ion-input> <div *ngIf="tel.invalid && (tel.dirty || tel.touched)" style="color: red;"> <div *ngIf="tel.errors?.['required']"> Phone is required </div> <div *ngIf="tel.errors?.['forbiddenName']"> Enter Phone </div> </div> </ion-item> </ion-list> </ion-list> </ion-card-content> <ion-card-header> <ion-button color="danger" (click)="onSubmit()">Send form</ion-button> </ion-card-header> </ion-card> </div> </ion-content> <div *ngIf="showProfile==true"> <ion-list> <h2>Your data sent successfuly</h2> <ion-list> <ion-item> Text {{forms.text}} </ion-item> <ion-item> Number {{forms.number}} </ion-item> <ion-item> Password {{forms.password}} </ion-item> <ion-item> Email {{forms.email}} </ion-item> <ion-item> Phone {{forms.tel}} </ion-item> </ion-list> </ion-list> </div>

Here, we've added an ionChange event that triggers a method called onInputChange() whenever the input value changes. You can use similar events like ionBlur, ionFocus, etc., to interact with the input field as needed.

Overall, ion-input is a powerful and flexible component that can be customized to suit a wide range of input needs in your Ionic application.

Post a Comment

Previous Post Next Post