Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Simple Textbox validation

Simple Textbox validation

Scheduled Pinned Locked Moved WPF
wpfhelp
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aslesh
    wrote on last edited by
    #1

    Hi i have a simple text box in window. I just want to write a simple validation for the text box. If there is nothing in the text box it should show a validation error. or else nothing. Can any one please provide the code for this.. Full XAML file. and Cs file Santhapur

    P L 2 Replies Last reply
    0
    • A Aslesh

      Hi i have a simple text box in window. I just want to write a simple validation for the text box. If there is nothing in the text box it should show a validation error. or else nothing. Can any one please provide the code for this.. Full XAML file. and Cs file Santhapur

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Santhapur wrote:

      Can any one please provide the code for this.. Full XAML file. and Cs file

      That's not the way this site works you know. We don't actually write your code for you.

      Deja View - the feeling that you've seen this post before.

      My blog | My articles | MoXAML PowerToys

      A 1 Reply Last reply
      0
      • A Aslesh

        Hi i have a simple text box in window. I just want to write a simple validation for the text box. If there is nothing in the text box it should show a validation error. or else nothing. Can any one please provide the code for this.. Full XAML file. and Cs file Santhapur

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Santhapur wrote:

        Can any one please provide the code for this

        No but I will give a hint, you are looking for ValidationRule

        Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert. Sometimes it takes a lot of work to be lazy Individuality is fine, as long as we do it together - F. Burns

        1 Reply Last reply
        0
        • P Pete OHanlon

          Santhapur wrote:

          Can any one please provide the code for this.. Full XAML file. and Cs file

          That's not the way this site works you know. We don't actually write your code for you.

          Deja View - the feeling that you've seen this post before.

          My blog | My articles | MoXAML PowerToys

          A Offline
          A Offline
          Aslesh
          wrote on last edited by
          #4

          Thanks for your Reply. I have asked u the full code b/c previously some one posted code snippet and its not working.. So i am not able to find where the error was.. I am not going to earn money with u r code.. i am still in learning stage.

          Pete O'Hanlon wrote:

          That's not the way this site works you know. We don't actually write your code for you.

          P 1 Reply Last reply
          0
          • A Aslesh

            Thanks for your Reply. I have asked u the full code b/c previously some one posted code snippet and its not working.. So i am not able to find where the error was.. I am not going to earn money with u r code.. i am still in learning stage.

            Pete O'Hanlon wrote:

            That's not the way this site works you know. We don't actually write your code for you.

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            Asking for full code breaches the spirit of the site - and you'll get short shrift for it. We give you help, and pointers, we don't write the code. If you're given a snippet and it doesn't work, reply to the poster and ask for clarification - that's the normal etiquette. You'll see that most of the solutions in the forums, are in the form of snippets or advice on where to look. That's just the way it is - because you will learn best by actually solving the problem for yourself.

            Deja View - the feeling that you've seen this post before.

            My blog | My articles | MoXAML PowerToys

            A 1 Reply Last reply
            0
            • P Pete OHanlon

              Asking for full code breaches the spirit of the site - and you'll get short shrift for it. We give you help, and pointers, we don't write the code. If you're given a snippet and it doesn't work, reply to the poster and ask for clarification - that's the normal etiquette. You'll see that most of the solutions in the forums, are in the form of snippets or advice on where to look. That's just the way it is - because you will learn best by actually solving the problem for yourself.

              Deja View - the feeling that you've seen this post before.

              My blog | My articles | MoXAML PowerToys

              A Offline
              A Offline
              Aslesh
              wrote on last edited by
              #6

              Here is what i have tried.. namespace TextBoxValidation { class validatetextbox : ValidationRule { public string MatchText { get; set; } public override ValidationResult Validate(object value, CultureInfo info) { if ((string)value == MatchText) return new ValidationResult(true, null); return new ValidationResult(false, "This is not a match"); } } } Here are my results.. http://schemas.microsoft.com/winfx/2006/xaml/presentation'. Line 9 Position 18

              P 1 Reply Last reply
              0
              • A Aslesh

                Here is what i have tried.. namespace TextBoxValidation { class validatetextbox : ValidationRule { public string MatchText { get; set; } public override ValidationResult Validate(object value, CultureInfo info) { if ((string)value == MatchText) return new ValidationResult(true, null); return new ValidationResult(false, "This is not a match"); } } } Here are my results.. http://schemas.microsoft.com/winfx/2006/xaml/presentation'. Line 9 Position 18

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                Well, the first thing I can see is that you've called the class validatetextbox, but it's referred to as MatchRule. Rename the class to MatchRule. Also, I don't see a reference to xmlns:local in the Window declaration.

                Deja View - the feeling that you've seen this post before.

                My blog | My articles | MoXAML PowerToys

                A 1 Reply Last reply
                0
                • P Pete OHanlon

                  Well, the first thing I can see is that you've called the class validatetextbox, but it's referred to as MatchRule. Rename the class to MatchRule. Also, I don't see a reference to xmlns:local in the Window declaration.

                  Deja View - the feeling that you've seen this post before.

                  My blog | My articles | MoXAML PowerToys

                  A Offline
                  A Offline
                  Aslesh
                  wrote on last edited by
                  #8

                  xmlns:local="clr-namespace:TextBoxValidation" I have included these two line but still getting the same error

                  P 1 Reply Last reply
                  0
                  • A Aslesh

                    xmlns:local="clr-namespace:TextBoxValidation" I have included these two line but still getting the same error

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #9

                    The reason you are getting this error is because you haven't specified valid XAML code. Basically, you want to change your XAML so that you are binding your textbox to something, and set your validation rule on it like so:

                    <TextBox>
                    <Binding Path="Name">
                    <Binding.ValidationRules>
                    <local:MatchRule MatchText="Hello" />
                    </Binding.ValidationRules>
                    </Binding>
                    </TextBox>

                    Take a look at this[^] article for a detailed example.

                    Deja View - the feeling that you've seen this post before.

                    My blog | My articles | MoXAML PowerToys

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups