\d{0,6}(\.\d{1,2})?
Main Topics
Browse All TopicsHi
I am using (\d{1,6})(\.[0-9]{1,2})? to enter a decimal value. But, when the number is just a fraction, the user will enter dot then 2 digits. Unfortunately, this is now working. He has to enter a zero before dot to be accepted.
How can I change it to add zero directly after he pressed dot to be 0.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
if you just want to allow users to enter .## format, your regular expression should be like this:
\d{0,6}(\.\d{1,2})?
But if you want to replace "." wih "0." if dot is the first key then you can handle the keypress event like this:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 46 && textBox1.Text.Length == 0)
{
e.Handled = true;
textBox1.Text = "0.";
textBox1.SelectionStart = textBox1.Text.Length+1;
}
}
and you can stll check the entered data, lets say on button click event like this:
private void btnTest_Click(object sender, EventArgs e)
{
if (Regex.IsMatch(textBox1.Te
{
label1.Text = Convert.ToDecimal(textBox1
}
}
Hope it helps
Regex alone can not replace "." with "0." while he data is getting entered unless you handle the keydown, keypress etc. events. if your goal is to replace the "." if it is the first character entered into to the txtbox then yes you should handle the keypress event but if you are ok to apply the regex to the entered data:
\d{0,6}(\.\d{1,2})?
above regex will do the job and once it's converted to decimal or double "0" will be automatically added to the entered value.
Business Accounts
Answer for Membership
by: kaufmedPosted on 2009-11-04 at 16:14:35ID: 25745599
You could change your regex to be:
(\d{,6})(\.[0-9]{1,2})?