Simple Textbox validation
-
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
-
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
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.
-
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
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
-
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.
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.
-
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.
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.
-
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.
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
-
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
Well, the first thing I can see is that you've called the class
validatetextbox
, but it's referred to asMatchRule
. Rename the class toMatchRule
. Also, I don't see a reference toxmlns:local
in theWindow
declaration.Deja View - the feeling that you've seen this post before.
-
Well, the first thing I can see is that you've called the class
validatetextbox
, but it's referred to asMatchRule
. Rename the class toMatchRule
. Also, I don't see a reference toxmlns:local
in theWindow
declaration.Deja View - the feeling that you've seen this post before.
-
xmlns:local="clr-namespace:TextBoxValidation" I have included these two line but still getting the same error
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.