none
How to make a textbox to allow Only decimals with regular expresion

    Question

  • Hi All,

     I have a  requirement to allow text box only decimals , it should not allow charecters , it can allow number and decimals .

    like

    22.34567 or 22 or 222222.1234567

    Thanks in Advance

    srilath


    Srilatha

    Friday, October 11, 2013 9:55 AM

Answers

  • Hi,

    Regarding the link I provided in the previous reply, it is a good sample of using regular expression. You need to focus ” User, LoginUser, and PasswordResetUser” and modified something according to your requirements.

    For example, we can have a look at this:

    // Metadata classes are not meant to be instantiated.
            protected UserMetadata()
            {
            }
    
            [Display(Name = "UserNameLabel", ResourceType = typeof(IssueVisionResources))]
            [Required(ErrorMessageResourceName = "ValidationErrorRequiredField", 
                      ErrorMessageResourceType = typeof(ErrorResources))]
            [RegularExpression("^[a-zA-Z0-9_]*$", 
             ErrorMessageResourceName = "ValidationErrorInvalidUserName", 
             ErrorMessageResourceType = typeof(ErrorResources))]
    

    The regular expression limits the type of input and the error message resource will display when we input incorrectly.

    Hope it helps.



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 24, 2013 11:39 AM

All replies

  • Hi Srilatha,

    According to your description, I’d like to suggest you to use Regular Expression to control the type of input. We can judge if the input meets the type we required.

    There is a sample code below for your reference:

    xaml:

    <TextBox HorizontalAlignment="Left" Height="65" Margin="65,171,0,0" x:Name="textBox" AcceptsReturn="True"
                     TextWrapping="Wrap"  VerticalAlignment="Top" Width="132" TextChanged="textBox_TextChanged"/>

    xaml.cs:

    private string pattern = @"^[0-9]|[0-9].[0-9]*$";
    private string param1 = "it's a wrong type,please input a decimal type number";
    
    private void textBox_TextChanged(object sender, TextChangedEventArgs e)
            {
                Match m = Regex.Match(this.textBox.Text, pattern);   
    
                if (!m.Success)
                {
                    this.textBox.Text = param1;
                }
            }

    I hope it helps.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, October 14, 2013 6:09 AM
  • Hi yunjie Pei,

     Thanks in advance for your reply , it is working allmost correct , textbox not allowing enter to Alphabets like A, B, C ,D so on as first charecters but when i enter like 22.88ABC in this case allowing which I dont need .

      and I am following MVVM patern and giving validations in Service.metadata.cs  , how can I use this regural expression in metadata class . My sample code as bellow  

    RegularExpression(@"^[0-9]|[0-9].[0-9]*$")]

    [Display(Name = "Call Loans ")]

    public decimal CallLoansForecast { get; set; }

    Kindly help me

    Regrds

    Srilatha M


    Srilatha

    Monday, October 14, 2013 9:22 AM
  • Hi Srilatha,

    According to your description, I tested the code and have modified the regular expression.

    Please check the regular expression:[RegularExpression (@"^\d*\.?\d{0,}$")] if it works well.

    In addition, there is an article about Silverlight MVVM. It introduces using regular expression in metadata class. You can refer to the link below for more details:

    A Sample Silverlight 4 Application Using MEF, MVVM, and WCF RIA Services - Part 3

    http://www.codeproject.com/Articles/83078/A-Sample-Silverlight-4-Application-Using-MEF-MVVM

    Hope it helps.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 16, 2013 1:11 PM
  • Hi Yunjie Pei ,

      Thanks in advance for great support . 

        It is working in bellow case

     private string pattern = @"^\d*\.?\d{0,}$";  //[RegularExpression(@"^\d*\.?\d{0,}$")]   @"^[0-9]|[0-9].[0-9]*$"
            private string param1 = "it's a wrong type,please input a decimal type number";

            private void CallLoansForecastTextBox_TextChanged(object sender, TextChangedEventArgs e)
            {
                Match m = Regex.Match(this.CallLoansForecastTextBox.Text, pattern);

                if (!m.Success)
                {
                   this.CallLoansForecastTextBox.Text = param1;
                }
            }

      here it is allowing to enter all characters like A,B, C  and then throwing error message.

    when go with regular expression not working .  

    Regards

    Srilatha



    Srilatha


    Friday, October 18, 2013 9:53 AM
  • Hi ,

    Help me any one ,

     My requirment is textbox must allow only 1 to 9 and one DOT (.) , this should be done by REGULAR EXPRESSION. IN Any position of the text box should not allow alphabets

    If ithe above way is not possible kindly advice any alternate way to accomplish this.

    Note: Iam using MVVM patern , iam not much intested to write any Text Box events  at code behind.

    Regards

    Srilatha


    Srilatha


    • Edited by Ramesh Raja Wednesday, October 23, 2013 2:53 PM
    Wednesday, October 23, 2013 2:51 PM
  • Hi,

    Regarding the link I provided in the previous reply, it is a good sample of using regular expression. You need to focus ” User, LoginUser, and PasswordResetUser” and modified something according to your requirements.

    For example, we can have a look at this:

    // Metadata classes are not meant to be instantiated.
            protected UserMetadata()
            {
            }
    
            [Display(Name = "UserNameLabel", ResourceType = typeof(IssueVisionResources))]
            [Required(ErrorMessageResourceName = "ValidationErrorRequiredField", 
                      ErrorMessageResourceType = typeof(ErrorResources))]
            [RegularExpression("^[a-zA-Z0-9_]*$", 
             ErrorMessageResourceName = "ValidationErrorInvalidUserName", 
             ErrorMessageResourceType = typeof(ErrorResources))]
    

    The regular expression limits the type of input and the error message resource will display when we input incorrectly.

    Hope it helps.



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 24, 2013 11:39 AM