Iam facing a problem in setting the password pattern as "Password must contain atleast 8 letters one lowercase and one uppercase letter one digit and one special character". I already used validators pattern but its not working.
Below is my code:
html file:
<ion-content>
<div style="margin: 30px">
<ion-title class="ion-text-center" >Register</ion-title>
<form [formGroup]="loginForm">
<ion-item>
<ion-label position="floating">First Name*</ion-label>
<ion-input formControlName="fname" type="text" ></ion-input>
</ion-item>
<div class="error-messages">
<ng-container *ngFor="let error of error_messages.fname">
<div class="error-message" *ngIf="loginForm.get('fname').hasError(error.type) && (loginForm.get('fname').dirty || loginForm.get('fname').touched)">
{{ error.message }}
</div>
</ng-container>
</div>
<ion-item>
<ion-label position="floating" >Last Name*</ion-label>
<ion-input formControlName="lname" type="text" ></ion-input>
</ion-item>
<div class="error-messages">
<ng-container *ngFor="let error of error_messages.lname">
<div class="error-message" *ngIf="loginForm.get('lname').hasError(error.type) && (loginForm.get('lname').dirty || loginForm.get('lname').touched)">
{{ error.message }}
</div>
</ng-container>
</div>
<ion-item>
<ion-label position="floating" >Email Id*</ion-label>
<ion-input formControlName="email" type="text" ></ion-input>
</ion-item>
<div class="error-messages">
<ng-container *ngFor="let error of error_messages.email">
<div class="error-message" *ngIf="loginForm.get('email').hasError(error.type) && (loginForm.get('email').dirty || loginForm.get('email').touched)">
{{ error.message }}
</div>
</ng-container>
</div>
<ion-item>
<ion-label position="floating" >Password*</ion-label>
<ion-input formControlName="password" type="password" ></ion-input>
</ion-item>
<div class="error-messages">
<ng-container *ngFor="let error of error_messages.password">
<div class="error-message" *ngIf="loginForm.get('password').hasError(error.type) && (loginForm.get('password').dirty || loginForm.get('password').touched)">
{{ error.message }}
</div>
</ng-container>
</div>
<br>
<div>
<ion-button [disabled]="!loginForm.valid" expand="full">Signup</ion-button>
</div>
.ts file:
import { Component, OnInit } from '#angular/core';
import { FormGroup, FormBuilder, FormControl, Validators } from '#angular/forms';
#Component({
selector: 'app-login',
templateUrl: './login.page.html',
styleUrls: ['./login.page.scss'],
})
export class LoginPage implements OnInit {
loginForm: FormGroup;
error_messages = {
'fname': [
{ type: 'required', message: 'First Name is required.'},
],
'lname': [
{ type: 'required', message: 'Last Name is required.'}
],
'email': [
{ type: 'required', message: 'Email is required.'},
{ type: 'required', message: 'please enter a valid email address.'}
],
'password': [
{ type: 'required', message: 'password is required.'},
{ type: 'pattern', message: 'Password must be contain atleast 8 letters one lowercase and one uppercase letter one digit and one special character.'}
],
}
constructor(
public formBuilder: FormBuilder
) {
this.loginForm = this.formBuilder.group({
fname: new FormControl('', Validators.compose([
Validators.required
])),
lname: new FormControl('', Validators.compose([
Validators.required
])),
email: new FormControl('', Validators.compose([
Validators.required,
])),
password: new FormControl('', Validators.compose([
Validators.required,
Validators.minLength(8),
Validators.maxLength(30),
Validators.pattern('^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9]+$')
])),
});
}
ngOnInit() {
}
}
Enter password should contain atleast 8 charecters, one upper case, one lower case, one numner and one special charecter.
Pleasehelp me on this.
thank you.
html .file
<ion-item>
<ion-label position="floating">
<img src="../assets/icon/key-icon.png"/>
<!-- <ion-icon name="mail" mode="ios" color="primary" "></ion-icon> -->
Password
</ion-label>
<ion-input [type]="passwordType" formControlName="password"></ion-input>
<ion-icon name="eye" [color]="passwordShown === true ? 'primary' : 'light'" slot="end" (click)="togglePassword()"></ion-icon>
</ion-item>
<ion-item no-padding lines="none" class="validator-error"
*ngIf="registration_form.controls.password.hasError('required') && registration_form.controls.password.touched">
<p>Please Enter a Password!</p>
</ion-item>
<ion-item no-padding lines="none" class="validator-error"
*ngIf="registration_form.controls.password.hasError('minlength') && registration_form.controls.password.touched">
<p>The password needs at least 8 characters.</p>
</ion-item>
<ion-item no-padding lines="none" class="validator-error"
*ngIf="registration_form.controls.password.hasError('pattern') && registration_form.controls.password.touched">
<p>Please Enter One Upper Case and One Lower Case, One Spacial Characters and One Number</p>
</ion-item>
ts.file
password: [null, Validators.compose([
Validators.required,
Validators.pattern('(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$#$!%*?&])[A-Za-z\d$#$!%*?&].{7,}'),
Validators.minLength(8)
])]
Just add the below in the validators pattern
Validators.pattern('(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$#$!%*?&])[A-Za-z\d$#$!%*?&].{7,}'),
Related
I prefilled ion-input with a value but it is not displaying. I am new to ionic but I understand little of it concepts.
Here is what I tried.
.ts
async getSimData() {
try {
let simPermission = await this.sim.requestReadPermission();
if (simPermission == "OK") {
let simData = await this.sim.getSimInfo();
this.simInfo = simData;
this.cards = simData.cards;
}
} catch (error) {
console.log(error);
}
}
html
<ion-card *ngFor= "let card of cards" >
<ion-card-content>
<ion-list>
<ion-item>
<ion-input type="number" name="phone" value="{{ card.phoneNumber }}" #phone disabled="true"></ion-input>
</ion-item>
</ion-list>
</ion-card-content>
</ion-card>
How can I make this appear in the ion-input?
you can also use with ngModel, i checked your code it is also working with value.
In below code both are working, check this : https://stackblitz.com/edit/ionic-eu7tcl
<ion-card *ngFor= "let card of cards" >
<ion-card-content>
<ion-list>
<ion-item>
<ion-input type="number" name="phone" [(ngModel)]="card.phoneNumber" #phone disabled="true"></ion-input>
<ion-input type="number" name="phone" value="{{ card.phoneNumber }}" #phone disabled="true"></ion-input>
</ion-item>
</ion-list>
</ion-card-content>
</ion-card>
I have the below form that is generate dynamically, I am trying to make a validation but is fail
Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'model: sdaDAd'. Current value: 'model: '.
HTML
<div [formGroup]="someForm">
<ion-item-group *ngFor="let att of day; let idx = index">
<ion-item >
<ion-label color="primary">{{att.label}}{{idx+1}}. Day</ion-
label>
<ion-input formControlName="day" type="text" text-right
[(ngModel)]="day[idx].value"></ion-input>
</ion-item>
<ion-item >
<ion-label color="primary">{{att.label}}{{idx+1}}. Exc</ion-label>
<ion-input type="text" formControlName="exc" text-right
[(ngModel)]="exc[idx].value"></ion-input>
</ion-item>
<ion-item >
<ion-label color="primary">{{att.label}}{{idx+1}}.
Hint/Repeats</ion-label>
<ion-input type="text" formControlName="hint" text-right
[(ngModel)]="hint[idx].value"></ion-input>
</ion-item>
<span color=danger float-right ion-button icon-left clear
*ngIf="exc.length > 0"
(click)="removeInputField(idx)"><ion-icon name="close"></ion-icon>
Remove
</span>
</ion-item-group>
<ion-card *ngIf="data_exc">
<ion-item *ngFor="let att of exc; let i=index">
<div class="card-title">Exc: {{att.value}}</div>
<div class="card-title">Hint/Repeats: {{hint[i].value}}</div>
<div class="card-title">Day: {{day[i].value}}</div>
</ion-item>
<div class="card-title">Notes: {{workoutData.notes}}</div>
</ion-card>
<button ion-button (click)="Add()">+Add</button>
<button ion-button (click)="goTo()" >Preview</button> <br>
<ion-toolbar>
<ion-item>
<ion-textarea placeholder="Tap here to enter a new note"
[(ngModel)]="workoutData.notes" formControlName="notes"
autocomplete="on" autocorrect="on"></ion-textarea>
</ion-item>
</ion-toolbar>
<button [disabled]="!someForm.valid" ion-button full
(click)="presentConfirmCustomWorkout()" type="submit" >Submit</button>
</div>
Validation to check if the form is empty so I will not get empty values in array
this.someForm = formbuilder.group({
'day': [this.day.value, Validators.compose([Validators.required])],
'exc': [this.exc.value, Validators.compose([Validators.required])],
'hint': [this.hint.value, Validators.compose([Validators.required])],
'notes': ['', Validators.compose([Validators.required])],
});
Here the video i am following this video, I did same as video, but still showing me error undefined value. anyone had idea what to do. Please help me and i am using ionic 3,
Here the video i am following this video, I did same as video, but still showing me error undefined value. anyone had idea what to do. Please help me and i am using ionic 3,
home.html
<ion-header>
<ion-navbar>
<ion-title>
Ionic Blank
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-list>
<ion-item>
<ion-label floating>Username</ion-label>
<ion-input type="text" name="username" ></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Password</ion-label>
<ion-input type="password" name="password"></ion-input>
</ion-item>
</ion-list>
<div padding>
<button ion-button color="primary" (click)="signIn()" block>Sign In</button>
</div>
</ion-content>
home.ts
import { Component, ViewChild } from '#angular/core';
import { NavController, AlertController } from 'ionic-angular';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
#ViewChild('username') uname;
#ViewChild('passwoed') password;
constructor(public navCtrl: NavController, public alertCtrl:
AlertController ) {
}
signIn() {
console.log(this.uname.value, this.password.value)
if(this.uname.value == "admin" && this.password.value == "admin") {
let alert = this.alertCtrl.create({
title: 'Login Successfull!',
subTitle: 'You are logged in!',
buttons: ['OK']
});
alert.present();
}
}
}
You got mistake on home.html, i resolve this.
<ion-header>
<ion-navbar>
<ion-title>
Ionic Blank
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-list>
<ion-item>
<ion-label floating>Username</ion-label>
<ion-input type="text" #username ></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Password</ion-label>
<ion-input type="password" #password></ion-input>
</ion-item>
</ion-list>
<div padding>
<button ion-button color="primary" (click)="signIn()" block>Sign In</button>
</div>
</ion-content>
Im using ionic 3 for my mobile application, I have some issues with my input fields which do not move automatically to the next field. For example when I click the first input filed and fill the first one, the cursor does not move to the next field. How to do that correctly?
<ion-grid>
<ion-row>
<ion-col>
<ion-item >
<ion-input type="tel" placeholder="*" maxlength="1" tabindex="1" ></ion-input>
</ion-item>
</ion-col>
<ion-col >
<ion-item>
<ion-input type="tel" placeholder="*" maxlength="1" tabindex="2" ></ion-input>
</ion-item>
</ion-col>
<ion-col >
<ion-item>
<ion-input type="tel" placeholder="*" maxlength="1" tabindex="3" ></ion-input>
</ion-item>
</ion-col>
<ion-col >
<ion-item>
<ion-input type="tel" placeholder="*" maxlength="1" tabindex="4" ></ion-input>
</ion-item>
</ion-col>
</ion-row>
</ion-grid>
You can use the following approach, there could be better approaches i'm just sharing what i know.
What i am doing here is setting a reference of the next element (eg: #b), and on keyup event i am passing that reference to my function in .ts file which only calls the .setFocus() on the referenced element.
<ion-grid>
<ion-row>
<ion-col>
<ion-item >
<ion-input type="tel" placeholder="*" maxlength="1" tabindex="1" (keyup)="moveFocus(b)" ></ion-input>
</ion-item>
</ion-col>
<ion-col >
<ion-item>
<ion-input type="tel" placeholder="*" maxlength="1" tabindex="2" #b (keyup)="moveFocus(c)" ></ion-input>
</ion-item>
</ion-col>
<ion-col >
<ion-item>
<ion-input type="tel" placeholder="*" maxlength="1" tabindex="3" #c (keyup)="moveFocus(d)" ></ion-input>
</ion-item>
</ion-col>
<ion-col >
<ion-item>
<ion-input type="tel" placeholder="*" maxlength="1" tabindex="4" #d ></ion-input>
</ion-item>
</ion-col>
</ion-row>
</ion-grid>
.ts:
import { Component } from '#angular/core';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor() {
}
moveFocus(nextElement) {
nextElement.focus();
}
}
You can try something with the (keyup) or (keydown) events. There is also documentation available on the angular docs that talks about forms and user Input.
HTML
<input (keyup)="onKey($event)">
.ts
onKey(event) {
if (event.key === "Enter") {
console.log(event);
}
}
This can be achieved by using the nextElementSibling , you can use the Keyup event to achieve this , here is the Pseudo code
Add keyup event to the event fields as follows (keyup)="keytab($event)"
keytab(event){
let element = event.srcElement.nextElementSibling; // get the sibling element
if(element == null) // check if its null
return;
else
element.focus(); // focus if not null
}
In order to make it more clear manner you can create a directive also as follows
tabindex.directive.ts:
import { Directive, HostListener, Input } from '#angular/core';
import { TextInput } from 'ionic-angular';
#Directive({
selector: '[yourTabindex]'
})
export class TabindexDirective {
constructor(private inputRef: TextInput) { }
#HostListener('keydown', ['$event']) onInputChange(e) {
var code = e.keyCode || e.which;
if (code === 13) {
e.preventDefault();
this.inputRef.focusNext();
}
}
}
Don't forget to include this directive in Modules page and you can use it on your input fields as follows ,as a sample:
<ion-input type="tel" placeholder="*" maxlength="1" tabindex="1" yourTabindex ></ion-input>
Adding "scrollAssist: true" and "autoFocusAssist: true" to app.module in the #ngModule would solve this problem.
imports: [
IonicModule.forRoot(MyApp, {
scrollAssist: true,
autoFocusAssist: true
})
],
I am working on form input field i am able to display errors if the user enters wrong.
What i am expecting is when after the user complete is text in the input field and after that i should get the error message.
but i am getting the error message shown when user started is typing
here is my html code
<form [formGroup]="myForm" (ngSubmit)="submit()" >
<ion-row>
<ion-col>
<ion-item>
<ion-label primary floating>
FIRST NAME
</ion-label>
<ion-input type="text" id="firstname" class="form-control" formControlName="firstname"></ion-input>
</ion-item>
</ion-col>
<ion-col>
<ion-item>
<ion-label primary floating>
LAST NAME
</ion-label>
<ion-input type="text" id="lastname" class="form-control" formControlName="lastname"></ion-input>
</ion-item>
</ion-col>
</ion-row>
<ion-item>
<ion-label primary floating>
USER ID (PHONE NO)
</ion-label>
<ion-input type="number" id="useridphone" class="form-control" formControlName="useridphone"></ion-input>
</ion-item>
<div *ngIf="submitAttempt">
<p *ngIf="myForm.controls.useridphone.errors && myForm.controls.useridphone.dirty " >
<small class="up" >
<strong><i>
Phone Number Must be 10 digits!
</i></strong>
</small>
</p>
</div>
<ion-item>
<ion-label primary floating>
PASSWORD
</ion-label>
<ion-input type="password" id="password" class="form-control" formControlName="password"></ion-input>
</ion-item>
<p *ngIf="myForm.controls.password.errors && myForm.controls.password.dirty">
<small class="up">
<strong><i>
Password must contain atleast (4),1-Char 1-Number
</i></strong>
</small>
</p>
<ion-item>
<ion-label primary floating>
CONFIRM PASSWORD
</ion-label>
<ion-input type="password" id="confirmpassword" class="form-control" formControlName="confirmpassword" ></ion-input>
</ion-item>
<ion-item>
<ion-label primary floating>
EMAIL
</ion-label>
<ion-input type="email" id="email" class="form-control" formControlName="email" ></ion-input>
</ion-item>
<p *ngIf="myForm.controls.email.errors && myForm.controls.email.dirty " class="alert alert-danger">
<small class="up">
<strong> <i>
Please Enter Valid Email Address!
</i></strong>
</small>
</p>
<div padding> </div>
<div padding> </div>
<button ion-button full round type="submit" [disabled]="!myForm.valid" color="secondary">SIGN UP</button> <br>
</form>
here is my ts file
passwordRegex: any = '(^(?=.*[a-z])(?=.*[0-9])[a-zA-Z0-9]+$)' ;
emailRegex: any = '^[a-z0-9]+(\.[_a-z0-9]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,15})$';
this.myForm = new FormGroup({
'firstname' : new FormControl('',[Validators.required,Validators.minLength(3),Validators.maxLength(10)]),
'lastname' : new FormControl('', [Validators.required,Validators.minLength(1),Validators.maxLength(10)]),
'useridphone' : new FormControl('', [Validators.required,Validators.minLength(10),Validators.maxLength(10)]),
'password' : new FormControl('',Validators.compose([Validators.required,Validators.minLength(4),Validators.maxLength(25),
Validators.pattern(this.passwordRegex)])),
'confirmpassword': new FormControl('',Validators.required),
'email' : new FormControl( '', Validators.compose([ Validators.required, Validators.pattern(this.emailRegex) ]) )
})
Use "onblur" event of HTML element.
<input type="text" name="name" value="value" onblur="validateText ( this )"/>
validateText will run when the user "leaves" the input field.
(User later indicated this was Ionic specific).
onBlur is (as far as I can see) an open issue in Ionic. It doesn't work. The recommendation from the github thread on this:
Here is a current work around for "blur".
Use "focusout" or "mouseleave". Those are both working.
https://github.com/driftyco/ionic/issues/5487
Note that in the github thread, it says that post beta-4 of Ionic, "blur" is working.