Click here to Skip to main content
15,887,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am using a regular exression to validate numeric value in TextBox for my Windows Application in C#.Net.
This is my regular expression
private void txtNumeric_TextChanged(object sender, EventArgs e)
       {
            Regex regex = new Regex(@"^*[0-9,-]+\.?[0-9]*$");
            if (!(regex.IsMatch(txtNumeric.Text)))
            {
                txtNumeric.ResetText();
            }
        }


It is working well but the problem is, it is also accepting "-2-3-2-3".
I need my regex to accept '-' only at first position and not later.
Means '-' should be optional only at first postion.

How should i modify my regex to fulfill above condition?

Thanks and Regards,
Nagendra.
Posted
Updated 29-Aug-10 20:40pm
v4

Use ^-*[0-9]+\.?[0-9]*$


As you have improved the question, I see you are using JavaScript codes to validate. You don't need it. Better you use a RegularExpressionValidator.

I used the following code. You may try something similar:

XML
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
      <asp:RegularExpressionValidator ControlToValidate="TextBox1" ID="RegularExpressionValidator1" ValidationExpression="^-*[0-9]+\.?[0-9]*$"  runat="server" ErrorMessage="Invalid input."></asp:RegularExpressionValidator>


I've tested, this is working. Please let me know if there is anything further problem.
 
Share this answer
 
v2
Comments
nagendrathecoder 30-Aug-10 2:23am    
Sorry, but it is not accepting '-' anywhere now.
I have updated my question and added a bit more code, so that u'll get an idea what i am doing.
Al-Farooque Shubho 30-Aug-10 2:33am    
I've modified my answer. Actually, I used a RegularExpressionValidator control to do the validation. This is easier and smarter.
nagendrathecoder 30-Aug-10 2:35am    
Thanks for your efforts but unfortunately i am using Windows application and don't have Validation Controls at my disposal.
Al-Farooque Shubho 30-Aug-10 2:37am    
Oops! You should have mentioned that earlier :)
If u need Regex for allowing numeric data it will be


Regex regex = new Regex(@"^(\+|-)?\d+$");


As per your requirement

Regex regex = new Regex(@"^[-+]?\d*\.?\d*$");

This one is correct.
 
Share this answer
 
v5
Comments
nagendrathecoder 30-Aug-10 2:43am    
Do you really think that it'll fulfill the condition i mentioned?
I don't think so.
Regex is always better than such code snippets.
nagendrathecoder 30-Aug-10 2:44am    
Reason for my vote of 2
Not the answer i am looking for.
demouser743 30-Aug-10 2:45am    
It will just allow numeric data and will ommit all other
demouser743 30-Aug-10 2:50am    
Added an Regex for allowing numeric data
demouser743 30-Aug-10 2:53am    
Given the Regex for your required check once

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900