validation

This commit is contained in:
2018-04-25 20:12:10 -04:00
parent 5db5cf17e1
commit 0e4ceac959
8 changed files with 12 additions and 111 deletions

View File

@@ -2717,7 +2717,7 @@ class PHP_CRUD_API {
'dbengine'=>'MySQL',
'hostname'=>'localhost',
'username'=>'lazyp_workadmin',
'password'=>'ZGmH0HQPo5ocbOuN',
'password'=>'GH5fZF0iCtLnHLrz',
'database'=>'LudosData',
'charset'=>'utf8mb4'
));

View File

@@ -26,8 +26,6 @@ import { LoginComponent } from './login/login.component';
import { RegisterComponent } from './register/register.component';
import { RegistrationService } from './registration.service';
//import { CreditCardValidator } from './creditcardvalidator.directive'
import { UsernameValidator } from './validators/username.validator'

View File

@@ -1,31 +0,0 @@
import { Directive, forwardRef } from '@angular/core';
import { NG_VALIDATORS, AbstractControl, ValidationErrors, Validator, FormControl } from '@angular/forms';
@Directive({
selector: '[validCreditCard]',
providers: [
{ provide: NG_VALIDATORS, useExisting: CreditCardValidator, multi: true }
]
})
export class CreditCardValidator implements Validator {
validate(c: FormControl): ValidationErrors | null {
return CreditCardValidator.validateCcNumber(c);
}
static validateCcNumber(control: FormControl): ValidationErrors | null {
if (!(control.value.startsWith('37')
|| control.value.startsWith('4')
|| control.value.startsWith('5'))
) {
// Return error if card is not Amex, Visa or Mastercard
return { creditCard: 'Your credit card number is not from a supported credit card provider' };
} else if (control.value.length !== 16) {
console.log(control.value)
// Return error if length is not 16 digits
return { creditCard: 'A credit card number must be 16-digit long' };
}
// If no error, return null
return null;
}
}

View File

@@ -1,56 +0,0 @@
import { Directive, forwardRef } from '@angular/core';
import { NG_VALIDATORS, AbstractControl, ValidationErrors, Validator, FormControl } from '@angular/forms';
import { RegistrationService } from './registration.service';
@Directive({
selector: '[validCreditCard]',
providers: [
{ provide: NG_VALIDATORS, useExisting: CreditCardValidatorX, multi: true }
]
})
export class CreditCardValidatorX implements Validator {
constructor(
private registrationService2: RegistrationService
){ }
validate(c: FormControl): ValidationErrors | null {
return CreditCardValidatorX.validateCcNumber(c);
}
validateUserName( control: FormControl ){
this.registrationService2.validateUserName( control.value ).subscribe( data => {
console.log( data );
if( control.value != data.username ){
return{
validUserName: true
};
}
return null;
});
}
static validateCcNumber(control: FormControl): ValidationErrors | null {
if (!(control.value.startsWith('37')
|| control.value.startsWith('4')
|| control.value.startsWith('5'))
) {
// Return error if card is not Amex, Visa or Mastercard
return { creditCard: 'Your credit card number is not from a supported credit card provider' };
} else if (control.value.length !== 16) {
console.log(control.value)
// Return error if length is not 16 digits
return { creditCard: 'A credit card number must be 16-digit long' };
}
// If no error, return null
return null;
}
}

View File

@@ -20,7 +20,7 @@ const httpOptionsPut = {
@Injectable()
export class GamesService {
APIURL = "http://192.241.155.78/ludosdata/api.php";
APIURL = "http://192.241.155.78/api.php";
constructor(
private http: HttpClient

View File

@@ -6,8 +6,6 @@ import { RegistrationService } from '../registration.service';
import { UsernameValidator } from '../validators/username.validator'
//import { ValidateUserName } from '../validators/username.validator';
@Component({
selector: 'app-register',
templateUrl: './register.component.html',
@@ -17,7 +15,6 @@ export class RegisterComponent implements OnInit {
form: any;
loading = false;
//userNameValidator = new UserNameValidator(null);
constructor(
private registrationService: RegistrationService,
@@ -79,8 +76,6 @@ export class RegisterComponent implements OnInit {
}else if( key == "max" ) {
formValidators.push(Validators.max(validators[key]));
}else if( key == "ValidateUserName" ){
//formValidators.push( this.validateUserName );
//formValidators.push( CreditCardValidator.validateCcNumber );
formValidators.push( this.usernameValidator.checkUsername.bind( this.usernameValidator ) );
}
}

View File

@@ -7,7 +7,7 @@ import 'rxjs/add/operator/map';
@Injectable()
export class RegistrationService {
APIURL = "http://192.241.155.78/ludosdata/api.php";
APIURL = "http://192.241.155.78/api.php";
constructor(
private http: HttpClient
@@ -25,8 +25,9 @@ export class RegistrationService {
*/
validateUserName( userName ): Observable<any> {
return this.http.get( this.APIURL + "/users?filter=email,cs," + userName + "&transform=1" )
return this.http.get( this.APIURL + "/users?filter=userName,cs," + userName + "&transform=1" )
.map(res => {
console.log(res)
return(
res
);

View File

@@ -7,18 +7,15 @@ export class UsernameValidator {
debouncer: any;
constructor(private registrationService: RegistrationService){
}
checkUsername(control: FormControl): any {
constructor(
private registrationService: RegistrationService
){ }
checkUsername( control: FormControl ): any{
clearTimeout(this.debouncer);
return new Promise(resolve => {
this.debouncer = setTimeout(() => {
this.registrationService.validateUserName(control.value).subscribe((res) => {
if(res.ok){
resolve(null);
@@ -26,10 +23,7 @@ export class UsernameValidator {
}, (err) => {
resolve({'usernameInUse': true});
});
}, 1000);
});
}
}