views:

59

answers:

3

Hello Good people

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

+1  A: 

The regex is fine and does what you want. Perhaps your text box has some stray whitespace in it?

Try encapsulating stray whitespace at the beginning and end, which is more robust anyways:

^\s*(0(2|5)[0-9]{3})?((,0(2|5)[0-9]{3})*?)\s*$
orangeoctopus
+2  A: 

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*$.
Guffa
Hello thanks for the advise `^(\s*(0[25]\d{3})?((,0[25]\d{3})*?)\s*)?$` or `^(\s*(0(2|5)[0-9]{8})?((,0(2|5)[0-9]{8})*?)\s*)?$` works but it's slightly different from your suggestion.thanks
black sensei
I see, you want to allow whitespace also. I added that to the answer. Both your patterns still contain `*?` to make a non-greedy match, which is pointless when you are matching a fixed size number.
Guffa
A: 

Under your view model, just set Numbers =string.Empty when the view model starts loading, so through the change notification, your logic should be applied and you will gain the effect you want.

Cheers.

DrakeVN