Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -5,3 +5,4 @@ export * from './mobile.validator';
|
||||
export * from './must-match.validator';
|
||||
export * from './password.validator';
|
||||
export * from './postal-code.validator';
|
||||
export * from './username.validator';
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { ValidatorFn } from '@angular/forms';
|
||||
|
||||
export function usernameValidator(): ValidatorFn {
|
||||
return (control) => {
|
||||
if (control.value === null || control.value === undefined || control.value === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (control.value.length < 6) {
|
||||
return {
|
||||
minlength: {
|
||||
requiredLength: 6,
|
||||
actualLength: control.value.length,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const pattern = /^[a-zA-Z0-9_-]*$/;
|
||||
|
||||
if (!pattern.test(control.value)) {
|
||||
return {
|
||||
invalidUsername: {
|
||||
value: control.value,
|
||||
message:
|
||||
'نام کاربری فقط میتواند شامل حروف انگلیسی، اعداد، خط تیره (-) و زیرخط (_) باشد',
|
||||
},
|
||||
};
|
||||
}
|
||||
return null;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user