Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
21
rated 0 times [  28] [ 7]  / answers: 1 / hits: 96403  / 13 Years ago, tue, june 21, 2011, 12:00:00

i'm making some input mask that allows only float number. But current problem is I can't check if multiple dots entered. Can you check those dots and prevent it for me?



Live Code: http://jsfiddle.net/thisizmonster/VRa6n/



$('.number').keypress(function(event) {
if (event.which != 46 && (event.which < 47 || event.which > 59))
{
event.preventDefault();
if ((event.which == 46) && ($(this).indexOf('.') != -1)) {
event.preventDefault();
}
}
});

More From » jquery

 Answers
6

You can check for the period in the same statement.


Also, you need to use the val method to get the value of the element.


Also, you want to check for the interval 48 to 57, not 47 to 59, otherwise you will also allow /, : and ;.




jQuery(document).ready(function() {
$('.float-number').keypress(function(event) {
if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
event.preventDefault();
}
});
});

<script src=https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js></script>
<html>
<body>
Enter Number:
<input type=text name=number value= class=float-number>
</body>
</html>




[#91601] Monday, June 20, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dahlias

Total Points: 730
Total Questions: 104
Total Answers: 101

Location: Denmark
Member since Tue, Jul 19, 2022
2 Years ago
;