How to only accept numbers like integer and float in an input field values using jQuery

How to only accept numbers like integer and float in an input field values using jQuery



I am having problems with my inputs having to accept only numbers like integers only and float, the problem is when everytime I input a value like "50.50", only "50.5" will be saved into the database, and when I input 500.50, "500.5" will be saved to the database, and if I input "50.0050" what will be saved to the database is "50.005".
how can I make the input fields with this class save "50.50" if I input 50.50 or "50.75" if input 50.75 and so on? and if there is a way that zero "0" and other characters like letters and punctuation marks other than period "." will automatically be removed upon input?



and one more thing, could this be possibly done after the user lost focus from the input field? or after every keypress perhaps?



this is what i have so far:


<script>
$(".digitOnly").each(function() {
var .digitOnly = $.isNumeric($(this).html())
$(this).val();
else
($(this).inputVal = "");
)
);
</script>



to those who have down-rated this post, you should have tried first your suggestion and the one I am trying to achieve as your suggested link still doesn't accept the value like "50.50".



but if you know such a result, I would greatly appreciate it than down-rating my post.



I am pertaining to this post:



How to only accept numbers like integer and float in an input field values using jQuery



this does not accept the input "50.50".





Possible duplicate of jquery only allow input float number
– Mr. Roshan
Sep 4 at 4:25





You can do this by html : <input type="number" placeholder="1.00" step="0.01" min="0" max="10">
– Vishnu Chauhan
Sep 4 at 5:42





no, it doesn't work.. i think i just have to use 2 separate fieLds indeed.
– makLoy
Sep 4 at 9:53




1 Answer
1



How any number, other than a string, appear in a database has really nothing to do with JQuery and everything to do with Database Design at the Field level. You need to specify Data Type and number of decimals for storage and display and stuff like that.



If you want to remember how digits are really added you need to add something like a String (varchar, etc), but then you need to have two fields for every one (one with the number and one with the string) or typecast the String version if you plan to use it in any numerical context.



On the loss of focus use:


$(this).blur(...



On after keypress use:


$(this).keyup(...



or


$(this).change(...



Thanks for contributing an answer to Stack Overflow!



But avoid



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:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)