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?
javascript jquery html regex
add a comment |
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?
javascript jquery html regex
<input type="number" step="0.01" />
– Andreas
yesterday
Try having a look at this JS library: openexchangerates.github.io/accounting.js
– Martin
yesterday
add a comment |
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?
javascript jquery html regex
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
javascript jquery html regex
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
add a comment |
<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
add a comment |
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/
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
add a comment |
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
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
add a comment |
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/
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
add a comment |
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/
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
add a comment |
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/
<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/
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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
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
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
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
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
<input type="number" step="0.01" />
– Andreas
yesterday
Try having a look at this JS library: openexchangerates.github.io/accounting.js
– Martin
yesterday