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 number upto 2 decimal points
Ranjit Powar
Nov 03
2014
Code
2.4
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
private
void
textbox_KeyPress(
object
sender, KeyPressEventArgs e)
{
if
(
char
.IsNumber(e.KeyChar) || e.KeyChar ==
'.'
)
{
int
len = textbox.Text.Length;
bool
hasDot = textbox.Text.Contains(
"."
);
if
(hasDot && e.KeyChar ==
'.'
)
{
e.Handled =
true
;
}
if
(Regex.IsMatch(textbox.Text,
"^\\d*\\.\\d{2}$"
)) e.Handled =
true
;
}
else
e.Handled = e.KeyChar != (
char
)Keys.Back;
}