Search code examples
c#wpfregexxamlmultidatatrigger

How to write a comma separated numbers regular expression validation for wpf textbox


i'm developing a WPF (.NET 3.5) application where i need to validate atextbox with a regular expression to match Empty textbox or text like 02145 or 05145 or 02145,05879,02445. the expression i use is ^(0(2|5)[0-9]{3})?((,0(2|5)[0-9]{3})*?)$.

It almost works just that i won't let me have empty textbox. here is some code

<Window.Resources>
    <data:Message x:Key="message"/>
    <Style x:Key="validButton" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}" >
        <Setter Property="IsEnabled" Value="False"/>
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <!-- ......-->
                    <Condition Binding="{Binding ElementName=txtNumbers, Path=(Validation.HasError)}" Value="false"/>
                </MultiDataTrigger.Conditions>
                <Setter Property="IsEnabled" Value="True" />
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>

<TextBox Height="23" Margin="-194.5,-88,-195.5,0" Name="txtNumbers" VerticalAlignment="Top" Style="{StaticResource txtboxerrors}">
                        <TextBox.Text>
                            <Binding Path="Numbers" Source="{StaticResource message}" ValidatesOnDataErrors="True" UpdateSourceTrigger="PropertyChanged">
                                <Binding.ValidationRules>
                                    <ExceptionValidationRule />
                                </Binding.ValidationRules>
                            </Binding>
                        </TextBox.Text>
 </TextBox>
<Button Height="23" Margin="0,0,-81,-189" Name="btnSendSMS" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="75" Click="btnSubmit_Click" Style="{StaticResource validButton}">Submit</Button>

And Class used for validation is below

class Message :IDataErrorInfo
{
    //...
    private string numbers;



    public string this[string columnName]
    {
        get
        {
            string result = null;
            //.....
            if (columnName == "Numbers")
            {
//multicellRegex has the ^(0(2|5)[0-9]{3})?((,0(2|5)[0-9]{3})*?)$ expression                 if(!Util.ValidateRegexPatern(Properties.Resources.multicellRegex,this.numbers))
                {
                    result = "Number not in the correct format.try 020xx or 05xxx,026xx";
                }
            }


            return result;
        }
    }

    public string Error
    {
        get { return  null; }
    }

   //.....
        public string Numbers
        {
          get { return numbers; }
          set { numbers = value; }
        }


}

this works well but then the submit button won't be active unless i type one or more numbers in the txtNumbers textbox.I just want it to allow empty textbox. How should i achieve that? Thanks for readking


Solution

  • You should make the entire pattern optional, not the separate parts, or it will think that ,02000 is a valid entry.

    As each number has a specific length, you don't need to make the match non-greedy (using *?).

    ^((0[25]\d{3})(,0[25]\d{3})*)?$.
    

    Allowing for whitespace around the numbers would be:

    ^\s*((0[25]\d{3})(\s*,\s*0[25]\d{3})*)?\s*$.