fixed regex for float and integer fields

This commit is contained in:
Maria Teresa Paratore 2024-06-20 17:01:32 +02:00
parent 8518810d34
commit d10e8ef3b5
2 changed files with 25 additions and 8 deletions

View File

@ -102,7 +102,7 @@
</mat-form-field> </mat-form-field>
<mat-form-field *ngIf="x.get('tipo')?.value === 'number'"> <mat-form-field *ngIf="x.get('tipo')?.value === 'number'">
<mat-label for="val">value</mat-label> <mat-label for="val">value</mat-label>
<input matInput formControlName="val" type="number"/> <input matInput formControlName="val" type="text"/>
<mat-error>{{checkForErrorsIn(facetTemplate.key,ind,i,'number')}}</mat-error> <mat-error>{{checkForErrorsIn(facetTemplate.key,ind,i,'number')}}</mat-error>
</mat-form-field> </mat-form-field>
<mat-form-field *ngIf="x.get('tipo')?.value === 'complexType'"> <mat-form-field *ngIf="x.get('tipo')?.value === 'complexType'">

View File

@ -189,13 +189,17 @@ defaultValue={new Date().toISOString().substring(0, (new Date().toISOString().in
valFc.setValue(''); valFc.setValue('');
valFc.setValidators([Validators.required]); valFc.setValidators([Validators.required]);
} }
if(newValue==='password'||newValue==='text'||newValue==='number'){ if(newValue==='password'||newValue==='text'){
valFc.setValue(''); valFc.setValue('');
valFc.setValidators([Validators.required]); valFc.setValidators([Validators.required]);
} }
if(newValue==='number'){
valFc.setValue('');
valFc.setValidators([Validators.required,Validators.pattern('^[-+]?[0-9]+$')]);
}
if(newValue==='float'){ if(newValue==='float'){
valFc.setValue(''); valFc.setValue('');
valFc.setValidators([Validators.required,Validators.pattern('^[-+]?[0-9]+\.[0-9]+$')]); valFc.setValidators([Validators.required,Validators.pattern('^[-+]?[0-9]*\.?[0-9]+$')]);
} }
} }
@ -229,31 +233,44 @@ defaultValue={new Date().toISOString().substring(0, (new Date().toISOString().in
checkForErrors(valFc:AbstractControl, inputType:string): string{ checkForErrors(valFc:AbstractControl, inputType:string): string{
let errorMsg: string; let errorMsg: string;
errorMsg=''; errorMsg='';
if(inputType==='number' && valFc.hasError('required')){
if(valFc.hasError('required')){ errorMsg = 'Input field is not an integer';
errorMsg = 'The field must not be empty'; return errorMsg;
}
if(inputType!=='number' && valFc.hasError('required')){
errorMsg ='The field must not be empty';
return errorMsg;
} }
if(!this.checkIfEmpty(valFc.value)&& inputType==='datetime'){ if(!this.checkIfEmpty(valFc.value)&& inputType==='datetime'){
if(!this.isIsoDate(valFc.value)){ if(!this.isIsoDate(valFc.value)){
errorMsg = 'Date is not in correct ISO format'; errorMsg = 'Date is not in correct ISO format';
return errorMsg;
} }
}else if(!this.checkIfEmpty(valFc.value)&& inputType==='complexType'){ }else if(!this.checkIfEmpty(valFc.value)&& inputType==='complexType'){
if(!this.isJsonString(valFc.value)){ if(!this.isJsonString(valFc.value)){
errorMsg = 'Please insert a valid JSON string'; errorMsg = 'Please insert a valid JSON string';
return errorMsg;
} }
}else }else
{ {
if(valFc.hasError('pattern')){ if(valFc.hasError('pattern')){
errorMsg = ''+ valFc.value+'is not a correct value'; if(!this.checkIfEmpty(valFc.value)&& inputType==='number'){
console.debug('++++++++ NAN - IS NUMBER +++++++');
errorMsg = valFc.value +' is not an integer';
return errorMsg;
}
if(!this.checkIfEmpty(valFc.value)&& inputType==='float'){ if(!this.checkIfEmpty(valFc.value)&& inputType==='float'){
errorMsg = 'input is not a decimal number (use . as a separator)'; errorMsg = 'not a number (use . as a separator)';
return errorMsg;
} }
if(!this.checkIfEmpty(valFc.value)&& inputType==='boolean'){ if(!this.checkIfEmpty(valFc.value)&& inputType==='boolean'){
errorMsg = 'please enter true or false'; errorMsg = 'please enter true or false';
return errorMsg;
} }
if(!this.checkIfEmpty(valFc.value)&& inputType==='date'){ if(!this.checkIfEmpty(valFc.value)&& inputType==='date'){
errorMsg = 'date or date format is not correct'; errorMsg = 'date or date format is not correct';
return errorMsg;
} }
} }
} }