diff --git a/npm/ng-packs/packages/core/src/lib/utils/common-utils.ts b/npm/ng-packs/packages/core/src/lib/utils/common-utils.ts index 31a1970fdc..b373b6c04a 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/common-utils.ts +++ b/npm/ng-packs/packages/core/src/lib/utils/common-utils.ts @@ -11,6 +11,10 @@ export function isNullOrUndefined(obj) { return obj === null || obj === undefined; } +export function isNullOrEmpty(obj){ + return obj === null || obj === undefined || obj === ''; +} + export function exists(obj) { return !isNullOrUndefined(obj); } diff --git a/npm/ng-packs/packages/core/src/lib/validators/index.ts b/npm/ng-packs/packages/core/src/lib/validators/index.ts index fb06e7f1d8..69880dbf84 100644 --- a/npm/ng-packs/packages/core/src/lib/validators/index.ts +++ b/npm/ng-packs/packages/core/src/lib/validators/index.ts @@ -5,6 +5,7 @@ import { validateRange } from './range.validator'; import { validateRequired } from './required.validator'; import { validateStringLength } from './string-length.validator'; import { validateUrl } from './url.validator'; +import { validateUsername } from './username.validator'; export * from './age.validator'; export * from './credit-card.validator'; export * from './range.validator'; @@ -20,4 +21,5 @@ export const AbpValidators = { required: validateRequired, stringLength: validateStringLength, url: validateUrl, + username: validateUsername, }; diff --git a/npm/ng-packs/packages/core/src/lib/validators/url.validator.ts b/npm/ng-packs/packages/core/src/lib/validators/url.validator.ts index 705d07e17a..da941aee6f 100644 --- a/npm/ng-packs/packages/core/src/lib/validators/url.validator.ts +++ b/npm/ng-packs/packages/core/src/lib/validators/url.validator.ts @@ -1,4 +1,5 @@ import { AbstractControl, ValidatorFn } from '@angular/forms'; +import { isNullOrUndefined } from '../utils'; export interface UrlError { url: true; @@ -6,11 +7,11 @@ export interface UrlError { export function validateUrl(): ValidatorFn { return (control: AbstractControl): UrlError | null => { - if (['', null, undefined].indexOf(control.value) > -1) return null; + if (isNullOrUndefined(control.value)) return null; return isValidUrl(control.value) ? null : { url: true }; }; -} +} function isValidUrl(value: string): boolean { if (/^http(s)?:\/\/[^/]/.test(value) || /^ftp:\/\/[^/]/.test(value)) { diff --git a/npm/ng-packs/packages/core/src/lib/validators/username.validator.ts b/npm/ng-packs/packages/core/src/lib/validators/username.validator.ts new file mode 100644 index 0000000000..5dfec72c8c --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/validators/username.validator.ts @@ -0,0 +1,29 @@ +import {AbstractControl, ValidatorFn} from '@angular/forms'; +import { isNullOrEmpty } from '../utils'; + +export interface UsernamePatternError { + usernamePattern: { + actualValue:string; + }; +} + +export interface UsernameOptions { + pattern?: RegExp; +} + +const onlyLetterAndNumberRegex = /^[a-zA-Z0-9]+$/; + +export function validateUsername( + {pattern}: UsernameOptions = {pattern: onlyLetterAndNumberRegex}, +): ValidatorFn { + return (control: AbstractControl): UsernamePatternError | null => { + const isValid = isValidUserName(control.value, pattern); + return isValid ? null : {usernamePattern: {actualValue: control.value}}; + }; +} + +function isValidUserName(value: any, pattern: RegExp): boolean { + if (isNullOrEmpty(value)) return true; + + return pattern.test(value); +} diff --git a/npm/ng-packs/packages/identity/src/lib/components/users/users.component.html b/npm/ng-packs/packages/identity/src/lib/components/users/users.component.html index 99d435c891..7873adf6ea 100644 --- a/npm/ng-packs/packages/identity/src/lib/components/users/users.component.html +++ b/npm/ng-packs/packages/identity/src/lib/components/users/users.component.html @@ -39,7 +39,7 @@ >