How to allow only numeric input, allow 2 digits after decimal and allow to input minus in jquery?











up vote
-1
down vote

favorite












I'd like to restrict users input in a form to a decimal with a maximum of two numbers after (.) And users allow to input minus. and a text field which only accepts a number.



<input type="text" name="debet" placeholder="debet" class=" money" value="0"/> 


Script:



$('body').on('focus',".money", function(){
$(this).simpleMoneyFormat();
});


Here is a jsFiddle example



Somebody can help me with this?










share|improve this question
























  • <input type="number" step="0.01" />
    – Andreas
    yesterday










  • Try having a look at this JS library: openexchangerates.github.io/accounting.js
    – Martin
    yesterday















up vote
-1
down vote

favorite












I'd like to restrict users input in a form to a decimal with a maximum of two numbers after (.) And users allow to input minus. and a text field which only accepts a number.



<input type="text" name="debet" placeholder="debet" class=" money" value="0"/> 


Script:



$('body').on('focus',".money", function(){
$(this).simpleMoneyFormat();
});


Here is a jsFiddle example



Somebody can help me with this?










share|improve this question
























  • <input type="number" step="0.01" />
    – Andreas
    yesterday










  • Try having a look at this JS library: openexchangerates.github.io/accounting.js
    – Martin
    yesterday













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I'd like to restrict users input in a form to a decimal with a maximum of two numbers after (.) And users allow to input minus. and a text field which only accepts a number.



<input type="text" name="debet" placeholder="debet" class=" money" value="0"/> 


Script:



$('body').on('focus',".money", function(){
$(this).simpleMoneyFormat();
});


Here is a jsFiddle example



Somebody can help me with this?










share|improve this question















I'd like to restrict users input in a form to a decimal with a maximum of two numbers after (.) And users allow to input minus. and a text field which only accepts a number.



<input type="text" name="debet" placeholder="debet" class=" money" value="0"/> 


Script:



$('body').on('focus',".money", function(){
$(this).simpleMoneyFormat();
});


Here is a jsFiddle example



Somebody can help me with this?







javascript jquery html regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









marvinIsSacul

8110




8110










asked yesterday









Sigit Prasetya

426




426












  • <input type="number" step="0.01" />
    – Andreas
    yesterday










  • Try having a look at this JS library: openexchangerates.github.io/accounting.js
    – Martin
    yesterday


















  • <input type="number" step="0.01" />
    – Andreas
    yesterday










  • Try having a look at this JS library: openexchangerates.github.io/accounting.js
    – Martin
    yesterday
















<input type="number" step="0.01" />
– Andreas
yesterday




<input type="number" step="0.01" />
– Andreas
yesterday












Try having a look at this JS library: openexchangerates.github.io/accounting.js
– Martin
yesterday




Try having a look at this JS library: openexchangerates.github.io/accounting.js
– Martin
yesterday












2 Answers
2






active

oldest

votes

















up vote
0
down vote













<input type="number" name="debet" placeholder="debet" class="money"/> 
var timer;

$('input').keyup(function (){
clearTimeout(timer);
timer = setTimeout(function (event) {
$('input').val(Number($('input').val()).toFixed(2))
}, 1000);
});


this should do, it will fire the event every 1 seconds



https://jsfiddle.net/usdf1nx6/






share|improve this answer





















  • thanks for answere, but automatic Number formatting not working. i also need Number formatting such as : XXX,XXX.XX
    – Sigit Prasetya
    yesterday










  • Like I had suggested to you via the edit (added regex tag), this is more of a regex question, bro. Try reading about regexes
    – marvinIsSacul
    yesterday


















up vote
0
down vote













You can add an event listener for blur event on input. And, then use a regex to validate the input using RegExp.test() method



let input = document.querySelector('input'); // input element
const checkRegex = /^-?d[,d]+d.d{0,2}$/; // regex for the format: DDD,DDD.DD

input.addEventListener('blur', evt => {
if(checkRegex.test(input.value)) {
input.style.borderColor = '#080'; // indicates correct input
} else {
input.style.borderColor = '#F46'; // indicates wrong input
}
});


Demo






share|improve this answer























  • thanks for answer, but your demo not working for me.
    – Sigit Prasetya
    yesterday










  • @SigitPrasetya can you explain what is not working for you?
    – rv7
    yesterday











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',
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
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203679%2fhow-to-allow-only-numeric-input-allow-2-digits-after-decimal-and-allow-to-input%23new-answer', 'question_page');
}
);

Post as a guest
































2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













<input type="number" name="debet" placeholder="debet" class="money"/> 
var timer;

$('input').keyup(function (){
clearTimeout(timer);
timer = setTimeout(function (event) {
$('input').val(Number($('input').val()).toFixed(2))
}, 1000);
});


this should do, it will fire the event every 1 seconds



https://jsfiddle.net/usdf1nx6/






share|improve this answer





















  • thanks for answere, but automatic Number formatting not working. i also need Number formatting such as : XXX,XXX.XX
    – Sigit Prasetya
    yesterday










  • Like I had suggested to you via the edit (added regex tag), this is more of a regex question, bro. Try reading about regexes
    – marvinIsSacul
    yesterday















up vote
0
down vote













<input type="number" name="debet" placeholder="debet" class="money"/> 
var timer;

$('input').keyup(function (){
clearTimeout(timer);
timer = setTimeout(function (event) {
$('input').val(Number($('input').val()).toFixed(2))
}, 1000);
});


this should do, it will fire the event every 1 seconds



https://jsfiddle.net/usdf1nx6/






share|improve this answer





















  • thanks for answere, but automatic Number formatting not working. i also need Number formatting such as : XXX,XXX.XX
    – Sigit Prasetya
    yesterday










  • Like I had suggested to you via the edit (added regex tag), this is more of a regex question, bro. Try reading about regexes
    – marvinIsSacul
    yesterday













up vote
0
down vote










up vote
0
down vote









<input type="number" name="debet" placeholder="debet" class="money"/> 
var timer;

$('input').keyup(function (){
clearTimeout(timer);
timer = setTimeout(function (event) {
$('input').val(Number($('input').val()).toFixed(2))
}, 1000);
});


this should do, it will fire the event every 1 seconds



https://jsfiddle.net/usdf1nx6/






share|improve this answer












<input type="number" name="debet" placeholder="debet" class="money"/> 
var timer;

$('input').keyup(function (){
clearTimeout(timer);
timer = setTimeout(function (event) {
$('input').val(Number($('input').val()).toFixed(2))
}, 1000);
});


this should do, it will fire the event every 1 seconds



https://jsfiddle.net/usdf1nx6/







share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









Darryl Ceguerra

435




435












  • thanks for answere, but automatic Number formatting not working. i also need Number formatting such as : XXX,XXX.XX
    – Sigit Prasetya
    yesterday










  • Like I had suggested to you via the edit (added regex tag), this is more of a regex question, bro. Try reading about regexes
    – marvinIsSacul
    yesterday


















  • thanks for answere, but automatic Number formatting not working. i also need Number formatting such as : XXX,XXX.XX
    – Sigit Prasetya
    yesterday










  • Like I had suggested to you via the edit (added regex tag), this is more of a regex question, bro. Try reading about regexes
    – marvinIsSacul
    yesterday
















thanks for answere, but automatic Number formatting not working. i also need Number formatting such as : XXX,XXX.XX
– Sigit Prasetya
yesterday




thanks for answere, but automatic Number formatting not working. i also need Number formatting such as : XXX,XXX.XX
– Sigit Prasetya
yesterday












Like I had suggested to you via the edit (added regex tag), this is more of a regex question, bro. Try reading about regexes
– marvinIsSacul
yesterday




Like I had suggested to you via the edit (added regex tag), this is more of a regex question, bro. Try reading about regexes
– marvinIsSacul
yesterday












up vote
0
down vote













You can add an event listener for blur event on input. And, then use a regex to validate the input using RegExp.test() method



let input = document.querySelector('input'); // input element
const checkRegex = /^-?d[,d]+d.d{0,2}$/; // regex for the format: DDD,DDD.DD

input.addEventListener('blur', evt => {
if(checkRegex.test(input.value)) {
input.style.borderColor = '#080'; // indicates correct input
} else {
input.style.borderColor = '#F46'; // indicates wrong input
}
});


Demo






share|improve this answer























  • thanks for answer, but your demo not working for me.
    – Sigit Prasetya
    yesterday










  • @SigitPrasetya can you explain what is not working for you?
    – rv7
    yesterday















up vote
0
down vote













You can add an event listener for blur event on input. And, then use a regex to validate the input using RegExp.test() method



let input = document.querySelector('input'); // input element
const checkRegex = /^-?d[,d]+d.d{0,2}$/; // regex for the format: DDD,DDD.DD

input.addEventListener('blur', evt => {
if(checkRegex.test(input.value)) {
input.style.borderColor = '#080'; // indicates correct input
} else {
input.style.borderColor = '#F46'; // indicates wrong input
}
});


Demo






share|improve this answer























  • thanks for answer, but your demo not working for me.
    – Sigit Prasetya
    yesterday










  • @SigitPrasetya can you explain what is not working for you?
    – rv7
    yesterday













up vote
0
down vote










up vote
0
down vote









You can add an event listener for blur event on input. And, then use a regex to validate the input using RegExp.test() method



let input = document.querySelector('input'); // input element
const checkRegex = /^-?d[,d]+d.d{0,2}$/; // regex for the format: DDD,DDD.DD

input.addEventListener('blur', evt => {
if(checkRegex.test(input.value)) {
input.style.borderColor = '#080'; // indicates correct input
} else {
input.style.borderColor = '#F46'; // indicates wrong input
}
});


Demo






share|improve this answer














You can add an event listener for blur event on input. And, then use a regex to validate the input using RegExp.test() method



let input = document.querySelector('input'); // input element
const checkRegex = /^-?d[,d]+d.d{0,2}$/; // regex for the format: DDD,DDD.DD

input.addEventListener('blur', evt => {
if(checkRegex.test(input.value)) {
input.style.borderColor = '#080'; // indicates correct input
} else {
input.style.borderColor = '#F46'; // indicates wrong input
}
});


Demo







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered yesterday









rv7

1,5991322




1,5991322












  • thanks for answer, but your demo not working for me.
    – Sigit Prasetya
    yesterday










  • @SigitPrasetya can you explain what is not working for you?
    – rv7
    yesterday


















  • thanks for answer, but your demo not working for me.
    – Sigit Prasetya
    yesterday










  • @SigitPrasetya can you explain what is not working for you?
    – rv7
    yesterday
















thanks for answer, but your demo not working for me.
– Sigit Prasetya
yesterday




thanks for answer, but your demo not working for me.
– Sigit Prasetya
yesterday












@SigitPrasetya can you explain what is not working for you?
– rv7
yesterday




@SigitPrasetya can you explain what is not working for you?
– rv7
yesterday


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203679%2fhow-to-allow-only-numeric-input-allow-2-digits-after-decimal-and-allow-to-input%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff