I currently have a number input script that behaves similar to a calculator with the way numbers are displayed, but I want to stop the adding of additional numbers to the series after two numbers past the decimal point.
Here is what I have so far, though the limiting doesn't occur correctly.
It should work like this:
1.25 - Allowed
12.5 - Allowed
125.5 - Allowed
125.55 - Allowed
123.555 - Not Allowed
rText = document.getElementById["resultText"];
function selectNumber[num] {
if [!rText.value || rText.value == "0"] {
rText.value = num;
}
else {
this part works...
rText.value += num;
}
}
}
but this part doesn't work... Any ideas???
if [rText.value.length - [rText.value.indexOf["."] + 1] > 2] {
return false;
} else {
rText.value += num;
}
}
}
asked Sep 20, 2017 at 12:08
var validate = function[e] {
var t = e.value;
e.value = [t.indexOf["."] >= 0] ? [t.substr[0, t.indexOf["."]] + t.substr[t.indexOf["."], 3]] : t;
}
answered Sep 20, 2017 at 12:32
Brahma DevBrahma Dev
1,8451 gold badge12 silver badges18 bronze badges
1
Save the previous value in some data attribute and if it exceeds 2 decimal places then restore the previous value
The 2 decimal places can be checked using Math.round[tis.value*100]/100!=tis.value
Note:
I have used oninput
to validate even in copy paste scenarios
function restrict[tis] {
var prev = tis.getAttribute["data-prev"];
prev = [prev != ''] ? prev : '';
if [Math.round[tis.value*100]/100!=tis.value]
tis.value=prev;
tis.setAttribute["data-prev",tis.value]
}
answered Sep 20, 2017 at 12:24
jafarbtechjafarbtech
6,4871 gold badge34 silver badges53 bronze badges
1
I love to use Math.floor and toFixed[] to resolve my decimal issues.
Here is a example:
var value = 123.5555
var roundedNumber = [Math.floor[value * 100] / 100].toFixed[2]
roundedNumber will be "123.55" as a string. So if you want as a number just add:
var value = 123.5555
var roundedNumber = Number[[Math.floor[value * 100] / 100].toFixed[2]]
and now you have value as a number and fixed to up 2 decimal places.
answered Sep 20, 2017 at 12:21
marquesm91marquesm91
5256 silver badges22 bronze badges
2
Just copy paste this method and call this method on your respective button on which button you have to check this decimal validation.
function CheckDecimal[inputtxt]
{
var decimal= /^[-+]?[0-9]+\.[0-9]+$/;
if[inputtxt.value.match[decimal]]
{
alert['Correct, try another...']
return true;
}
else
{
alert['Wrong...!']
return false;
}
}
Mister Jojo
17.8k3 gold badges16 silver badges39 bronze badges
answered Sep 20, 2017 at 12:40
I need validation on Rate like example
1-True
1.00-True
1.56-True
Means I need numeric values + point only
Validate two Decimal numbers with two decimal places using RegularExpression in JavaScript and jQuery
Download FREE API for Word, Excel and PDF in ASP.Net: Download
Hi amar,
Refer below sample code.
Javascript
function ValidateDecimal[ele] { //var regex = /^\d+[\.\d{1,2}]?$/; var regex = /[?:\d*\.\d{1,2}|\d+]$/; if [regex.test[ele.value]] { alert["Valid"]; } else { alert["Invalid"]; } }
Demo
jQuery
$[function [] { $["#txtText"].on["blur", function [] { //var regex = /^\d+[\.\d{1,2}]?$/; var regex = /[?:\d*\.\d{1,2}|\d+]$/; if [regex.test[$[this].val[]]] { alert["Valid"]; } else { alert["Invalid"]; } }]; }];
Demo