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
Restrict User to Enter Value up to 2 Decimal Places in JavaScript
Ranjit Powar
Nov 18
2015
Code
3
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
Html Input:
<input id=
"Input"
name=
"Quantity1"
type=
"text"
onkeypress=
"return IsDigit(event,this.id)"
/>
JavaScript:
<script type=
"text/javascript"
>
function IsDigit(evt, elementId)
{
var charCode = evt.charCode;
if
(charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46 && charCode != 0)
{
return
false
;
}
else
{
var len = document.getElementById(elementId).value.length;
var index = document.getElementById(elementId).value.indexOf(
'.'
);
if
(charCode == 0)
{
return
true
;
}
if
(index > 0 && charCode == 46)
{
return
false
;
}
if
(index > 0)
{
var CharAfterdot = (len + 1) - index;
if
(CharAfterdot > 3)
{
return
false
;
}
}
}
return
true
;
}
</script>
JavaScript
Validation
restrict User to Enter Value