Q:

How to flag a textbox input as invalid by using an attached behavior

I have many TextBox's on my xaml where I've implemented Regex validation (from
How to Define TextBox Resctrictions). In my view, this takes the form of:
 <TextBox 
             h:ColorMaskingTextBoxBehavior.Mask="^(?:\d{5})(?:-\d{4})?$"
             Text="{Binding ZipCode}"
            Height="21" HorizontalAlignment="Left" Margin="152,207,0,0"  VerticalAlignment="Top" Width="96"
             />

I do not wish to restrict the movement of the user so currently validation simply changes the colors of the textbox; red being an error was discovered.
After all input is gathered, the user is to click on Save to save the window.
The regex codes can be involved and it seems wrong to copy them from the xaml to the viewmodel where Save is executed. 
How can the attached behavior flag the textbox as invalid so that the viewmodel need only check the invalid flag before saving the window?  My first thought is to bind the Tag dependency property on the textbox but I wonder if there is a better way?
Any ideas are appreciated.
TIA

A:

I would move all of the validation to the view model, that's what it is there for. It's not good MVVM practice to have application logic in the view. Views should be interchangeable without needing to recode validation logic.
Then you could change the appearance of the TextBox using triggers within a style that bind to properties in the view model instead of using the attached behaviour.
Some people use IDataErrorInfo interface.. I don't.
Create a view model property that determines if ZipCodeIsValid:
public bool ZipCodeIsValid
{
    get
    {
        var zipCodeRegex = new Regex("^(?:\d{5})(?:-\d{4})?$");
        var zipCodeMatch = zipCodeRegex.Match(ZipCodeText);
        return zipCodeMatch.Success;    
    }
}

Every time ZipCodeText property changes, raise notify property changed event for ZipCodeIsValid property.
Create style for your TextBox:
<Style TargetType="TextBox">
    <Setter Property="Foreground" Value="Black"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding ZipCodeIsValid}" Value="False">
             <Setter Property="Foreground" Value="Black"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

If you don't want to make changes that big and are looking for a quicker solution, you could move the regex strings to static properties and use them with the attached behaviour, then you wouldn't be duplicating the regex and could use them in the view model to test that validation passes.
Create a class called ValidationRegex with static string property called ZipCode:
public class ValidationRegex
{
    public static string ZipCode = "^(?:\d{5})(?:-\d{4})?$";
}

Use it with attached behaviour:
<TextBox h:ColorMaskingTextBoxBehavior.Mask="{x:Static ValidationRegex.ZipCode}"/>

Use it in your view model:
public void Save()
{
    var zipCodeRegex = new Regex(ValidationRegex.ZipCode);
    var zipCodeMatch = zipCodeRegex.Match(ZipCodeText);
    if (!zipCodeMatch.Success)
    {
        throw new ValidationException("Zip code is invalid!");
    }
}

