TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Numeric validation on textbox using jQuery
Mohd Arif
Jul 28
2016
Code
2.6
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
<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