Angular 7 On value change of a drop list for a specific value, no validator ran (Conditional custom validators)










0















I am creating a conditional custom validator to check if a value of drop list changed, to add a new validator to another form control.



The drop list form control contain several value, and when the user change it to u_id I need to add to run a new validator on form control to take the u_id number according to a specific format:



<mat-select id="gov_id_type" formControlName="gov_id_type" placeholder="Gov. ID Type">
<mat-option value="birth_certificate">Birth Certificate</mat-option>
<mat-option value="regular_id">ID Card</mat-option>
<mat-option value="u_id">UID</mat-option>
<mat-select>


And the form control where the new validator should be added when selecting u_id:



<mat-form-field color="warn" appearance="outline">
<mat-label>Gov. ID Number</mat-label>
<input matInput id="gov_id_number" formControlName="gov_id_number" placeholder="Gov. ID Number">
</mat-form-field>&nbsp;


I created the custom validator to check if the gov_id_number has the following format if u_id selected, else, the system can accept any type of format:



import AbstractControl from '@angular/forms';

export function uIdValidation(control: AbstractControl)

const regex = /^[A-Z][0-9]3-[0-9]2[C]1[0-9]5$/i;
let val = control.value;
if(!regex.test(val))

return response: true;

return null;



The regex /^[A-Z][0-9]3-[0-9]2[C]1[0-9]5$/i means that I need the id to be like:




Q12-12C12345




The first 3 letters are alphanumeric with capital alpha values, then a - then 2 digits followed by C letter then 5 digits at the end.



I made a typescript function using valueChanges and setValidator:



runValidator()
let gov_id_number = this.formGroup.get('gov_id_number');
this.formGroup.controls['gov_id_type'].valueChanges.subscribe(
(mode: String)=>
console.log(mode)
if(mode=='u_id')

gov_id_number.setValidators([uIdValidation])

else

gov_id_number.clearValidators()


);



Of course, uIdValidation is imported:



import uIdValidation from 'src/app/dashboard/custom-validators/unIdValidation';


And I added runValidator() into ngOnInit() hook as mentioned in this tutorial



When I change the drop list into u_id value, the validator does not run immediately. I should select different value, then the u_id so the validator could run as expected. And another problem is that when I change it again from u_id to other values, the validator is not cleared.



I tried to run the function on (selectionChange) event on the drop list:



<mat-select id="gov_id_type" (selectionChange)="runValidator()" formControlName="gov_id_type" placeholder="Gov. ID Type">


It didn't work. If I selected the u_id at the beginning it won't run the validator, and when I select it again it works.



Here is a stackblitz.



I think there is a problem with the RegEx too.










share|improve this question
























  • It seems your regex should be /^[A-Z][0-9]2-[0-9]2C[0-9]5$/. Your sample says Q12, two digits after first letter, but you have [0-9]3, i.e. you require three digits after the first letter. Also, are you sure you need the case insensitive modifier, i? You say "capital letters", so, I would not suggest using i here.

    – Wiktor Stribiżew
    Nov 13 '18 at 10:22
















0















I am creating a conditional custom validator to check if a value of drop list changed, to add a new validator to another form control.



The drop list form control contain several value, and when the user change it to u_id I need to add to run a new validator on form control to take the u_id number according to a specific format:



<mat-select id="gov_id_type" formControlName="gov_id_type" placeholder="Gov. ID Type">
<mat-option value="birth_certificate">Birth Certificate</mat-option>
<mat-option value="regular_id">ID Card</mat-option>
<mat-option value="u_id">UID</mat-option>
<mat-select>


And the form control where the new validator should be added when selecting u_id:



<mat-form-field color="warn" appearance="outline">
<mat-label>Gov. ID Number</mat-label>
<input matInput id="gov_id_number" formControlName="gov_id_number" placeholder="Gov. ID Number">
</mat-form-field>&nbsp;


I created the custom validator to check if the gov_id_number has the following format if u_id selected, else, the system can accept any type of format:



import AbstractControl from '@angular/forms';

export function uIdValidation(control: AbstractControl)

const regex = /^[A-Z][0-9]3-[0-9]2[C]1[0-9]5$/i;
let val = control.value;
if(!regex.test(val))

return response: true;

return null;



The regex /^[A-Z][0-9]3-[0-9]2[C]1[0-9]5$/i means that I need the id to be like:




Q12-12C12345




The first 3 letters are alphanumeric with capital alpha values, then a - then 2 digits followed by C letter then 5 digits at the end.



I made a typescript function using valueChanges and setValidator:



runValidator()
let gov_id_number = this.formGroup.get('gov_id_number');
this.formGroup.controls['gov_id_type'].valueChanges.subscribe(
(mode: String)=>
console.log(mode)
if(mode=='u_id')

gov_id_number.setValidators([uIdValidation])

else

gov_id_number.clearValidators()


);



Of course, uIdValidation is imported:



import uIdValidation from 'src/app/dashboard/custom-validators/unIdValidation';


And I added runValidator() into ngOnInit() hook as mentioned in this tutorial



When I change the drop list into u_id value, the validator does not run immediately. I should select different value, then the u_id so the validator could run as expected. And another problem is that when I change it again from u_id to other values, the validator is not cleared.



I tried to run the function on (selectionChange) event on the drop list:



<mat-select id="gov_id_type" (selectionChange)="runValidator()" formControlName="gov_id_type" placeholder="Gov. ID Type">


It didn't work. If I selected the u_id at the beginning it won't run the validator, and when I select it again it works.



Here is a stackblitz.



I think there is a problem with the RegEx too.










share|improve this question
























  • It seems your regex should be /^[A-Z][0-9]2-[0-9]2C[0-9]5$/. Your sample says Q12, two digits after first letter, but you have [0-9]3, i.e. you require three digits after the first letter. Also, are you sure you need the case insensitive modifier, i? You say "capital letters", so, I would not suggest using i here.

    – Wiktor Stribiżew
    Nov 13 '18 at 10:22














0












0








0








I am creating a conditional custom validator to check if a value of drop list changed, to add a new validator to another form control.



The drop list form control contain several value, and when the user change it to u_id I need to add to run a new validator on form control to take the u_id number according to a specific format:



<mat-select id="gov_id_type" formControlName="gov_id_type" placeholder="Gov. ID Type">
<mat-option value="birth_certificate">Birth Certificate</mat-option>
<mat-option value="regular_id">ID Card</mat-option>
<mat-option value="u_id">UID</mat-option>
<mat-select>


And the form control where the new validator should be added when selecting u_id:



<mat-form-field color="warn" appearance="outline">
<mat-label>Gov. ID Number</mat-label>
<input matInput id="gov_id_number" formControlName="gov_id_number" placeholder="Gov. ID Number">
</mat-form-field>&nbsp;


I created the custom validator to check if the gov_id_number has the following format if u_id selected, else, the system can accept any type of format:



import AbstractControl from '@angular/forms';

export function uIdValidation(control: AbstractControl)

const regex = /^[A-Z][0-9]3-[0-9]2[C]1[0-9]5$/i;
let val = control.value;
if(!regex.test(val))

return response: true;

return null;



The regex /^[A-Z][0-9]3-[0-9]2[C]1[0-9]5$/i means that I need the id to be like:




Q12-12C12345




The first 3 letters are alphanumeric with capital alpha values, then a - then 2 digits followed by C letter then 5 digits at the end.



I made a typescript function using valueChanges and setValidator:



runValidator()
let gov_id_number = this.formGroup.get('gov_id_number');
this.formGroup.controls['gov_id_type'].valueChanges.subscribe(
(mode: String)=>
console.log(mode)
if(mode=='u_id')

gov_id_number.setValidators([uIdValidation])

else

gov_id_number.clearValidators()


);



Of course, uIdValidation is imported:



import uIdValidation from 'src/app/dashboard/custom-validators/unIdValidation';


And I added runValidator() into ngOnInit() hook as mentioned in this tutorial



When I change the drop list into u_id value, the validator does not run immediately. I should select different value, then the u_id so the validator could run as expected. And another problem is that when I change it again from u_id to other values, the validator is not cleared.



I tried to run the function on (selectionChange) event on the drop list:



<mat-select id="gov_id_type" (selectionChange)="runValidator()" formControlName="gov_id_type" placeholder="Gov. ID Type">


It didn't work. If I selected the u_id at the beginning it won't run the validator, and when I select it again it works.



Here is a stackblitz.



I think there is a problem with the RegEx too.










share|improve this question
















I am creating a conditional custom validator to check if a value of drop list changed, to add a new validator to another form control.



The drop list form control contain several value, and when the user change it to u_id I need to add to run a new validator on form control to take the u_id number according to a specific format:



<mat-select id="gov_id_type" formControlName="gov_id_type" placeholder="Gov. ID Type">
<mat-option value="birth_certificate">Birth Certificate</mat-option>
<mat-option value="regular_id">ID Card</mat-option>
<mat-option value="u_id">UID</mat-option>
<mat-select>


And the form control where the new validator should be added when selecting u_id:



<mat-form-field color="warn" appearance="outline">
<mat-label>Gov. ID Number</mat-label>
<input matInput id="gov_id_number" formControlName="gov_id_number" placeholder="Gov. ID Number">
</mat-form-field>&nbsp;


I created the custom validator to check if the gov_id_number has the following format if u_id selected, else, the system can accept any type of format:



import AbstractControl from '@angular/forms';

export function uIdValidation(control: AbstractControl)

const regex = /^[A-Z][0-9]3-[0-9]2[C]1[0-9]5$/i;
let val = control.value;
if(!regex.test(val))

return response: true;

return null;



The regex /^[A-Z][0-9]3-[0-9]2[C]1[0-9]5$/i means that I need the id to be like:




Q12-12C12345




The first 3 letters are alphanumeric with capital alpha values, then a - then 2 digits followed by C letter then 5 digits at the end.



I made a typescript function using valueChanges and setValidator:



runValidator()
let gov_id_number = this.formGroup.get('gov_id_number');
this.formGroup.controls['gov_id_type'].valueChanges.subscribe(
(mode: String)=>
console.log(mode)
if(mode=='u_id')

gov_id_number.setValidators([uIdValidation])

else

gov_id_number.clearValidators()


);



Of course, uIdValidation is imported:



import uIdValidation from 'src/app/dashboard/custom-validators/unIdValidation';


And I added runValidator() into ngOnInit() hook as mentioned in this tutorial



When I change the drop list into u_id value, the validator does not run immediately. I should select different value, then the u_id so the validator could run as expected. And another problem is that when I change it again from u_id to other values, the validator is not cleared.



I tried to run the function on (selectionChange) event on the drop list:



<mat-select id="gov_id_type" (selectionChange)="runValidator()" formControlName="gov_id_type" placeholder="Gov. ID Type">


It didn't work. If I selected the u_id at the beginning it won't run the validator, and when I select it again it works.



Here is a stackblitz.



I think there is a problem with the RegEx too.







regex angular typescript customvalidator angular7






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 9:42







alim1990

















asked Nov 13 '18 at 9:13









alim1990alim1990

98811332




98811332












  • It seems your regex should be /^[A-Z][0-9]2-[0-9]2C[0-9]5$/. Your sample says Q12, two digits after first letter, but you have [0-9]3, i.e. you require three digits after the first letter. Also, are you sure you need the case insensitive modifier, i? You say "capital letters", so, I would not suggest using i here.

    – Wiktor Stribiżew
    Nov 13 '18 at 10:22


















  • It seems your regex should be /^[A-Z][0-9]2-[0-9]2C[0-9]5$/. Your sample says Q12, two digits after first letter, but you have [0-9]3, i.e. you require three digits after the first letter. Also, are you sure you need the case insensitive modifier, i? You say "capital letters", so, I would not suggest using i here.

    – Wiktor Stribiżew
    Nov 13 '18 at 10:22

















It seems your regex should be /^[A-Z][0-9]2-[0-9]2C[0-9]5$/. Your sample says Q12, two digits after first letter, but you have [0-9]3, i.e. you require three digits after the first letter. Also, are you sure you need the case insensitive modifier, i? You say "capital letters", so, I would not suggest using i here.

– Wiktor Stribiżew
Nov 13 '18 at 10:22






It seems your regex should be /^[A-Z][0-9]2-[0-9]2C[0-9]5$/. Your sample says Q12, two digits after first letter, but you have [0-9]3, i.e. you require three digits after the first letter. Also, are you sure you need the case insensitive modifier, i? You say "capital letters", so, I would not suggest using i here.

– Wiktor Stribiżew
Nov 13 '18 at 10:22













0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277493%2fangular-7-on-value-change-of-a-drop-list-for-a-specific-value-no-validator-ran%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277493%2fangular-7-on-value-change-of-a-drop-list-for-a-specific-value-no-validator-ran%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3