Input field should allow numbers and decimal not minus symbol
<input type="number" pattern="[^[0-9]([.,][0-9]{1,3})?$]*">
Input field should allow numbers and decimal not minus symbol
<input type="number" pattern="[^[0-9]([.,][0-9]{1,3})?$]*">
OK, I got what your problem is. Please take a look at the section "Allowing decimal values". In order for an input field type="number"
to work with decimals, you need to change the step attribute.
Does this work as you intended?
First try:
<input type="number" pattern="\d+[.,]?\d+" step="0.001">
Second Try:
<input type="text" pattern="\d+|\d+[.,]\d{1,3}">
My suggestion to you is to get rid of the type=number
attribute. You already set a pattern
attribute for the input, unless you really want to keep the scrolls for plus or minus one step, I'd set type="text"
.
Try this:
<input type="number" pattern="/^[0-9.,]+$/">
You are looking for much readable and understandable code then this works perfectly for you:
function checkDecimalDot(e)
{
var charCode = (e.which) ? e.which : e.keyCode;
if (charCode != 46 && charCode > 31
&& (charCode < 48 || charCode > 57))
return false;
}
<input type="number" onkeypress="return checkDecimalDot(event)" >
This simple code will allow you to add only decimals and numbers without any negative value.
<input type="number" min="0" step="0.001">
It's done! Used the below code
@HostListener('keypress') onkeypress(e) {
let event = e || window.event;
let charCode = (event.which) ? event.which : event.keyCode;
let value : any = this.elementRef.nativeElement.value.replace(/[^0-9\.]/g, '');
if ((event.which != 46 || value.indexOf('.') != -1)
&& (event.which < 48 || event.which > 57)) {
event.preventDefault();
}
}