Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to restrict string value and
allow a text box to enter number followed by upto four decimal.
Eg.123.2345.plz provide the solution
Posted

I think this will help you.
^\d{1,3}(\.\d{0,4})?$
 
Share this answer
 
Comments
taher ahmed choudhury 13-Apr-11 3:55am    
whenever i enter "^d{1,3}( \.\d{0,4})?$" showing unrecognised escape sequence
Ashishmau 13-Apr-11 6:17am    
It is working
try this

If you want the user to be able to type 14 digits without any decimals or up to 11 digits with two decimals (and not allow just a single decimal digit), you could do this:
^(?:\d{1,14}|\d{1,11}\.\d\d)$


if your want decimal upto four places

^(?:\d{1,14}|\d{1,11}\.\d\d\d\d)$
 
Share this answer
 
Comments
taher ahmed choudhury 13-Apr-11 4:16am    
these are the code i used still showing error
check plz i checked with all
rev.ValidationExpression = " ^(?:\d{1,14}|\d{1,11}\.\d\d\d\d)$";"^[0-9]+[.]?[0-9]*$";//" ^(?:\d{1,14}|\d{1,11}\.\d\d\d\d)$" //"[0-9]+(.[0-9][0-9]?)?";//"[0-9]+";
hope i will get the solution soon. Thanks
you know what I dont really have that experience in regular expression but here is an answer that might help you

put this before the event

static int afterdot=0;
static bool dot=false;


at text changed event put this code

for(int i=0;i<textbox1.text;i++)>
{
   if(int.TryParse(textBox1.Text[i],int x))
   {
      if(dot)
      {
         afterdot++;
         if(afterd>4)
         {
            textBox1.Text=textBox1.Text.Remove(i);
         }
      } 
   }
   else
   {
      if(textBox1.Text[i]=="." && dot==false)
      {
         dot=true;
      }
      else
      {
         textBox1.Text=textBox1.Text.Remove(i);
      }
   }
}


I hope I helped
:-)
 
Share this answer
 
v2
toniyo jackson regular expression is working

u forgot to put @ before expression

u can do like this

C#
string id = "123.2345";
            string idexpr=@"^\d{1,3}(\.\d{0,4})?$";
            if(!Regex.IsMatch(id,idexpr))
            {
                MessageBox.Show("Invalid");
            }
 
Share this answer
 
But, sincerely, why code it? Why use complicated Regexes? Why disregard the wonderful work done by the Forms team? Why not use:
1. A NumericUpDown with Decimals = 4;?
2. A MaskedTextBox with Mask = "read the documentation / I don't use this Control often";?
 
Share this answer
 
<asp:TextBox ID="txtNo" Width="70px" runat="server"
MaxLength="7" Height="16px"></asp:TextBox>;
<cc1:FilteredTextBoxExtender ID="FilteredTextBoxExtender4" runat="server" FilterType="Numbers" TargetControlID="txtNo">
</cc1:FilteredTextBoxExtender>;
 
Share this answer
 
Comments
Toli Cuturicu 13-Apr-11 7:58am    
Unreadable non-sense. Shame.

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