Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Numeric validation on textbox using jQuery
WhatsApp
Mohd Arif
Jul 28
2016
2.7
k
0
1
<html>
<head>
<title>Number validation using jQuery
with
decimal and minus sign</title>
<script src=
"http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"
></script>
</head>
<body>
<div>
<h3>Numeric validation on
this
textbox</h3>
<input type=
"text"
class
=
"txtnumval"
/><br />
</div>
</body>
<script>
// JQUERY ".Class" SELECTOR.
$(document).ready(
function
() {
$(
'.txtnumval'
).keypress(
function
(event) {
return
isNumber(event,
this
)
});
});
// THE SCRIPT THAT CHECKS IF THE KEY PRESSED IS A NUMERIC OR DECIMAL VALUE.
function
isNumber(evt, element) {
var
charCode = (evt.which) ? evt.which : event.keyCode
if
(
(charCode != 45 || $(element).val().indexOf(
'-'
) != -1) &&
// “-” CHECK MINUS, AND ONLY ONE.
(charCode != 46 || $(element).val().indexOf(
'.'
) != -1) &&
// “.” CHECK DOT, AND ONLY ONE.
(charCode < 48 || charCode > 57))
return
false
;
return
true
;
}
</script>
</html>
validation
numeric input
Up Next
Numeric validation on textbox using jQuery