Validation error in angular input[type=number]?
I have a strange problem when trying to validate input[type=number]
in Angular 7 and hope that somebody can help.
<input class="form-control" type="number" name="entranceFee" min="0" [(ngModel)]="entranceFee" #entranceFee="ngModel" pattern="\d+" [ngClass]="{ 'is-invalid': entranceFee.touched && entranceFee.invalid }" >
<div *ngIf="entranceFee.invalid && entranceFee.touched" class="invalid-feedback">
<div *ngIf="entranceFee.errors.pattern">Only numbers are permitted</div>
<div *ngIf="entranceFee.errors.min">Value cannot be smaller than '0'</div>
</div>
The input shall only accept numbers >= 0
. But whenever I enter a number value (0-9) the form becomes invalid. Why? When I output the ngModel in the console in ngAfterViewChecked()
a number value was written to the model. So how can I validate this form? Is there a bug in the validator or in my head?
The same happens when implementing this code with reactive forms and/or with input[type=text]
angular validation angular2-template
add a comment |
I have a strange problem when trying to validate input[type=number]
in Angular 7 and hope that somebody can help.
<input class="form-control" type="number" name="entranceFee" min="0" [(ngModel)]="entranceFee" #entranceFee="ngModel" pattern="\d+" [ngClass]="{ 'is-invalid': entranceFee.touched && entranceFee.invalid }" >
<div *ngIf="entranceFee.invalid && entranceFee.touched" class="invalid-feedback">
<div *ngIf="entranceFee.errors.pattern">Only numbers are permitted</div>
<div *ngIf="entranceFee.errors.min">Value cannot be smaller than '0'</div>
</div>
The input shall only accept numbers >= 0
. But whenever I enter a number value (0-9) the form becomes invalid. Why? When I output the ngModel in the console in ngAfterViewChecked()
a number value was written to the model. So how can I validate this form? Is there a bug in the validator or in my head?
The same happens when implementing this code with reactive forms and/or with input[type=text]
angular validation angular2-template
It's a number input, so using pattern is useless. The input only accepts numbers. Andmin
is not a validation directive. It's only a standard html attribute that the browser uses to prevent you from spinning belos the minimum.
– JB Nizet
Dec 28 '18 at 9:51
not really, because it is possible to enter the letter "e". 1e1 makes sense but you can also enter e.g. "eeee"
– Lars Hagen
Dec 28 '18 at 14:42
add a comment |
I have a strange problem when trying to validate input[type=number]
in Angular 7 and hope that somebody can help.
<input class="form-control" type="number" name="entranceFee" min="0" [(ngModel)]="entranceFee" #entranceFee="ngModel" pattern="\d+" [ngClass]="{ 'is-invalid': entranceFee.touched && entranceFee.invalid }" >
<div *ngIf="entranceFee.invalid && entranceFee.touched" class="invalid-feedback">
<div *ngIf="entranceFee.errors.pattern">Only numbers are permitted</div>
<div *ngIf="entranceFee.errors.min">Value cannot be smaller than '0'</div>
</div>
The input shall only accept numbers >= 0
. But whenever I enter a number value (0-9) the form becomes invalid. Why? When I output the ngModel in the console in ngAfterViewChecked()
a number value was written to the model. So how can I validate this form? Is there a bug in the validator or in my head?
The same happens when implementing this code with reactive forms and/or with input[type=text]
angular validation angular2-template
I have a strange problem when trying to validate input[type=number]
in Angular 7 and hope that somebody can help.
<input class="form-control" type="number" name="entranceFee" min="0" [(ngModel)]="entranceFee" #entranceFee="ngModel" pattern="\d+" [ngClass]="{ 'is-invalid': entranceFee.touched && entranceFee.invalid }" >
<div *ngIf="entranceFee.invalid && entranceFee.touched" class="invalid-feedback">
<div *ngIf="entranceFee.errors.pattern">Only numbers are permitted</div>
<div *ngIf="entranceFee.errors.min">Value cannot be smaller than '0'</div>
</div>
The input shall only accept numbers >= 0
. But whenever I enter a number value (0-9) the form becomes invalid. Why? When I output the ngModel in the console in ngAfterViewChecked()
a number value was written to the model. So how can I validate this form? Is there a bug in the validator or in my head?
The same happens when implementing this code with reactive forms and/or with input[type=text]
angular validation angular2-template
angular validation angular2-template
asked Dec 28 '18 at 9:45
Lars HagenLars Hagen
263
263
It's a number input, so using pattern is useless. The input only accepts numbers. Andmin
is not a validation directive. It's only a standard html attribute that the browser uses to prevent you from spinning belos the minimum.
– JB Nizet
Dec 28 '18 at 9:51
not really, because it is possible to enter the letter "e". 1e1 makes sense but you can also enter e.g. "eeee"
– Lars Hagen
Dec 28 '18 at 14:42
add a comment |
It's a number input, so using pattern is useless. The input only accepts numbers. Andmin
is not a validation directive. It's only a standard html attribute that the browser uses to prevent you from spinning belos the minimum.
– JB Nizet
Dec 28 '18 at 9:51
not really, because it is possible to enter the letter "e". 1e1 makes sense but you can also enter e.g. "eeee"
– Lars Hagen
Dec 28 '18 at 14:42
It's a number input, so using pattern is useless. The input only accepts numbers. And
min
is not a validation directive. It's only a standard html attribute that the browser uses to prevent you from spinning belos the minimum.– JB Nizet
Dec 28 '18 at 9:51
It's a number input, so using pattern is useless. The input only accepts numbers. And
min
is not a validation directive. It's only a standard html attribute that the browser uses to prevent you from spinning belos the minimum.– JB Nizet
Dec 28 '18 at 9:51
not really, because it is possible to enter the letter "e". 1e1 makes sense but you can also enter e.g. "eeee"
– Lars Hagen
Dec 28 '18 at 14:42
not really, because it is possible to enter the letter "e". 1e1 makes sense but you can also enter e.g. "eeee"
– Lars Hagen
Dec 28 '18 at 14:42
add a comment |
1 Answer
1
active
oldest
votes
Give this a try:
<input
class="form-control"
type="number"
name="entranceFee"
#entranceFee="ngModel"
[(ngModel)]="entranceFee.value"
pattern="^[+]?([0-9]+(?:[.][0-9]*)?|.[0-9]+)$"
[ngClass]="{ 'is-invalid': entranceFee.touched && entranceFee.invalid }" >
<div *ngIf="entranceFee.invalid && entranceFee.touched" class="invalid-feedback">
<div *ngIf="entranceFee.errors.pattern">Only numbers are permitted</div>
<div *ngIf="entranceFee.errors.min">Value cannot be smaller than '0'</div>
</div>
Here's a Working Sample StackBlitz for your ref.
RegEx Courtesy - This Answer
The pattern is accept e. Ex. 3e3
– Manikandan Velayutham
Dec 28 '18 at 12:15
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53956477%2fvalidation-error-in-angular-inputtype-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Give this a try:
<input
class="form-control"
type="number"
name="entranceFee"
#entranceFee="ngModel"
[(ngModel)]="entranceFee.value"
pattern="^[+]?([0-9]+(?:[.][0-9]*)?|.[0-9]+)$"
[ngClass]="{ 'is-invalid': entranceFee.touched && entranceFee.invalid }" >
<div *ngIf="entranceFee.invalid && entranceFee.touched" class="invalid-feedback">
<div *ngIf="entranceFee.errors.pattern">Only numbers are permitted</div>
<div *ngIf="entranceFee.errors.min">Value cannot be smaller than '0'</div>
</div>
Here's a Working Sample StackBlitz for your ref.
RegEx Courtesy - This Answer
The pattern is accept e. Ex. 3e3
– Manikandan Velayutham
Dec 28 '18 at 12:15
add a comment |
Give this a try:
<input
class="form-control"
type="number"
name="entranceFee"
#entranceFee="ngModel"
[(ngModel)]="entranceFee.value"
pattern="^[+]?([0-9]+(?:[.][0-9]*)?|.[0-9]+)$"
[ngClass]="{ 'is-invalid': entranceFee.touched && entranceFee.invalid }" >
<div *ngIf="entranceFee.invalid && entranceFee.touched" class="invalid-feedback">
<div *ngIf="entranceFee.errors.pattern">Only numbers are permitted</div>
<div *ngIf="entranceFee.errors.min">Value cannot be smaller than '0'</div>
</div>
Here's a Working Sample StackBlitz for your ref.
RegEx Courtesy - This Answer
The pattern is accept e. Ex. 3e3
– Manikandan Velayutham
Dec 28 '18 at 12:15
add a comment |
Give this a try:
<input
class="form-control"
type="number"
name="entranceFee"
#entranceFee="ngModel"
[(ngModel)]="entranceFee.value"
pattern="^[+]?([0-9]+(?:[.][0-9]*)?|.[0-9]+)$"
[ngClass]="{ 'is-invalid': entranceFee.touched && entranceFee.invalid }" >
<div *ngIf="entranceFee.invalid && entranceFee.touched" class="invalid-feedback">
<div *ngIf="entranceFee.errors.pattern">Only numbers are permitted</div>
<div *ngIf="entranceFee.errors.min">Value cannot be smaller than '0'</div>
</div>
Here's a Working Sample StackBlitz for your ref.
RegEx Courtesy - This Answer
Give this a try:
<input
class="form-control"
type="number"
name="entranceFee"
#entranceFee="ngModel"
[(ngModel)]="entranceFee.value"
pattern="^[+]?([0-9]+(?:[.][0-9]*)?|.[0-9]+)$"
[ngClass]="{ 'is-invalid': entranceFee.touched && entranceFee.invalid }" >
<div *ngIf="entranceFee.invalid && entranceFee.touched" class="invalid-feedback">
<div *ngIf="entranceFee.errors.pattern">Only numbers are permitted</div>
<div *ngIf="entranceFee.errors.min">Value cannot be smaller than '0'</div>
</div>
Here's a Working Sample StackBlitz for your ref.
RegEx Courtesy - This Answer
answered Dec 28 '18 at 9:50
SiddAjmeraSiddAjmera
13.1k31137
13.1k31137
The pattern is accept e. Ex. 3e3
– Manikandan Velayutham
Dec 28 '18 at 12:15
add a comment |
The pattern is accept e. Ex. 3e3
– Manikandan Velayutham
Dec 28 '18 at 12:15
The pattern is accept e. Ex. 3e3
– Manikandan Velayutham
Dec 28 '18 at 12:15
The pattern is accept e. Ex. 3e3
– Manikandan Velayutham
Dec 28 '18 at 12:15
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53956477%2fvalidation-error-in-angular-inputtype-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
It's a number input, so using pattern is useless. The input only accepts numbers. And
min
is not a validation directive. It's only a standard html attribute that the browser uses to prevent you from spinning belos the minimum.– JB Nizet
Dec 28 '18 at 9:51
not really, because it is possible to enter the letter "e". 1e1 makes sense but you can also enter e.g. "eeee"
– Lars Hagen
Dec 28 '18 at 14:42