Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
fix(inlineinput): float value validation for user input (#2816)
Browse files Browse the repository at this point in the history
* Float field validation in inlineinput

* Float validation for inlineinput

* Fix-Float validation for inlineinput

* fix(inlineinput): Float value validation for user input

* fix(inlineinput): Float value validation for user input

* fix(inlineinput): Float value validation for user input
  • Loading branch information
abhinandan13jan authored and joshuawilson committed Dec 13, 2018
1 parent ca2af97 commit ca3a7d5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/app/widgets/inlineinput/inlineinput.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class InlineInputComponent implements OnInit {
});
} else {
this.errorMessage = `Invalid value for the field type ${this.type}`;
this.inputField.nativeElement.focus();
}
}

Expand All @@ -83,12 +84,17 @@ export class InlineInputComponent implements OnInit {
}
}

isFloat(val) {
let x = parseFloat(val);
return typeof x === 'number' && Number.isFinite(x) && x >= -2147483648 && x <= 2147483648;
}

validateValue(value) {
if (this.type === 'integer') {
return /^\d+$/.test(value);
}
if (this.type === 'float') {
return /^-?\d*(\.\d+)?$/.test(value);
return this.isFloat(value);
}
return true;
}
Expand Down Expand Up @@ -161,6 +167,9 @@ export class InlineInputComponent implements OnInit {
break;
}
}
if (this.editing && this.type == 'float' && keycode == 13) {
this.saveClick();
}
if (this.isNotValid) {
this.errorMessage = `This is a ${this.type} field`;
}
Expand Down
41 changes: 41 additions & 0 deletions src/app/widgets/inlineinput/inlineinput.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { async } from '@angular/core/testing';
import { InlineInputComponent } from './inlineinput.component';

describe('Unit Test :: Inline Input Float value', () => {
let comp = new InlineInputComponent();
beforeEach(async(() => {
comp.type = 'float';
}));

it('Should return true for input 2234.523 in float range', () => {
expect(comp.validateValue('2234.523')).toBe(true);
});

it('Should return true for input 1.2 in float range', () => {
expect(comp.validateValue('1.2')).toBe(true);
});

it('Should return true for input 223456.334 in float range', () => {
expect(comp.validateValue('223456.334')).toBe(true);
});

it('Should return true for input 45 in float range', () => {
expect(comp.validateValue('45')).toBe(true);
});

it('Should return false for input -34567812322.523 outside float range', () => {
expect(comp.validateValue('-34567812322.523')).toBe(false);
});

it('Should return false for input 345678123225.23678 outside float range', () => {
expect(comp.validateValue('345678123225.23678')).toBe(false);
});

it('Should return false for input stringtest outside float range', () => {
expect(comp.validateValue('stringtest')).toBe(false);
});

it('Should return false for input 11111111111111111.51 outside float range', () => {
expect(comp.validateValue('11111111111111111.51')).toBe(false);
});
});

0 comments on commit ca3a7d5

Please sign in to comment.