Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7548913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:49:04+00:00 2026-05-30T09:49:04+00:00

When my textbox is empty/null, i need to display Required. In my xaml: <TextBox

  • 0

When my textbox is empty/null, i need to display “Required”.

In my xaml:

  <TextBox Name="txtLastName"  Grid.Column="1"  Grid.Row="1"  Margin="3">
            <TextBox.Text>
                <Binding Path="LastName">
                    <Binding.ValidationRules>
                        <validators:Contractor
                                    MinimumLength="1" 
                                    MaximumLength="40"
                                    ErrorMessage="Required" />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>

In my class:

public string LastName
        {
            get { return _lastName; }
            set
            {
                _lastName = value;
              NotifyPropertyChanged("LastName");               

            }
        }   

public event PropertyChangedEventHandler PropertyChanged;

    private int _minimumLength = -1;
    private int _maximumLength = -1;
    private string _errorMessage;

    public int MinimumLength
    {
        get { return _minimumLength; }
        set { _minimumLength = value; }
    }

    public int MaximumLength
    {
        get { return _maximumLength; }
        set { _maximumLength = value; }
    }

    public string ErrorMessage
    {
        get { return _errorMessage; }
        set { _errorMessage = value; }
    }

    public override ValidationResult Validate(object value,CultureInfo cultureInfo)
    {
        ValidationResult result = new ValidationResult(true, null);
        string inputString = (value ?? string.Empty).ToString();
        if (inputString.Length < this.MinimumLength || value==null ||
               (this.MaximumLength > 0 &&
                inputString.Length > this.MaximumLength))
        {
            result = new ValidationResult(false, this.ErrorMessage);
        }
        return result;
    }


  private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

What i get is the textbox turns into red border when the data is null/empty and i am not able to see the “Required” error message, any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:49:05+00:00Added an answer on May 30, 2026 at 9:49 am

    (The red border is the default behavior of a TextBox when the attached property Validation.HasError is true.
    In order to display the error messsage you’ll have to do that yourself by binding to Validation.Errors
    . Validation.Errors is a list of error from each validator applied to the TextBox.
    Now in your case you only have one validator so in order to get the error message you need to bind to Validation.Errors[0].ErrorContent

    Example

    <StackPanel Orientation="Horizontal">
        <TextBox Name="txtLastName" Width="100">
            <TextBox.Text>
                <Binding Path="LastName">
                    <Binding.ValidationRules>
                        <validators:Contractor
                                    MinimumLength="1" 
                                    MaximumLength="40"
                                    ErrorMessage="Required" />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
        <!-- Show error message tot the right of the TextBox-->
        <TextBlock Text="{Binding (Validation.Errors)[0].ErrorContent, ElementName=txtLastName}"/>
    </StackPanel>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have the following XAML: <TextBox Text= x:Name=textFeedbackFeedback Margin=11.593,0,29.307,42.96 TextWrapping=Wrap VerticalContentAlignment=Top VerticalAlignment=Bottom Height=92.08
<TextBlock Text={Binding MyTextProperty}> <TextBlock.Style> <Style TargetType={x:Type TextBox}> <Style.Triggers> <DataTrigger Binding={Binding MyTextProperty} Value={x:Null}> <Setter Property=Text
I have this xaml textbox <TextBox Text={Binding ProdFilter.Min, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged, TargetNullValue=''} Width=50 DockPanel.Dock=Right TabIndex=3
I have two text box. If 1 of the textbox.text is empty, the MessageBox
I am having difficulty setting an empty TextBox to null on an nullable DB
I have a form: <StackPanel x:Name=LayoutRoot> <sdk:ValidationSummary /> <sdk:Label Target={Binding ElementName=Greeting} /> <TextBox x:Name=Greeting
I'm trying to check if asp.net textbox is empty or not using java script.
Html.TextBox(ParentPassword, , new { @class = required }) what the gosh darned heck is
The TextBox control offers a MaxLength property, which allows the insertable text into that
i'm developing a WPF (.NET 3.5) application where i need to validate a textbox

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.