Click here to Skip to main content
15,889,808 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: WPF decimal validation Pin
Mycroft Holmes28-Apr-11 14:16
professionalMycroft Holmes28-Apr-11 14:16 
GeneralRe: WPF decimal validation Pin
Pete O'Hanlon2-May-11 6:29
mvePete O'Hanlon2-May-11 6:29 
QuestionWPF textbox email validation Pin
arkiboys27-Apr-11 22:09
arkiboys27-Apr-11 22:09 
AnswerRe: WPF textbox email validation Pin
Pete O'Hanlon27-Apr-11 22:15
mvePete O'Hanlon27-Apr-11 22:15 
GeneralRe: WPF textbox email validation Pin
arkiboys27-Apr-11 22:46
arkiboys27-Apr-11 22:46 
GeneralRe: WPF textbox email validation Pin
Wayne Gaylard27-Apr-11 23:46
professionalWayne Gaylard27-Apr-11 23:46 
GeneralRe: WPF textbox email validation Pin
arkiboys28-Apr-11 0:36
arkiboys28-Apr-11 0:36 
GeneralRe: WPF textbox email validation Pin
Pete O'Hanlon27-Apr-11 23:51
mvePete O'Hanlon27-Apr-11 23:51 
arkiboys wrote:
However, I am trying to use the following method instead which is due to the
requirements...

What requirements do you have that say you have to provide your own preview check input? What you are trying to do here is actually poor form - if the user types in that doesn't match the regular expression you throw a message box up - it's better to prevent them from putting in an invalid value than telling them they got it wrong. BTW, you are using the preview key event here and then extracting the value from the textbox. In other words, if the user types in all valid characters bar the last one, your code won't catch it because it is reacting to the values that are already in the textbox, which does not include the key you've just pressed.

If what you are trying to do is just validate the input, then you need to implement IDataErrorInfo on your ViewModel, and then add the following to your binding in the textbox:
C#
UpdateSourceTrigger=LostFocus, ValidatesOnDataErrors=true, NotifyOnValidationError=true
By handling LostFocus, the validation will only occur when you focus away from the textbox. To add the validation in, you need to add the following code:
C#
public string Error
{
    get { throw new NotImplementedException(); }
}

public string this[string columnName]
{
    get
    {
      string result = null;
      if (columnName == "Email") // Assuming your property is called Email.
      {
        Regex regEMail = new Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
        bool isValid = regEmail.IsMatch(Email);
        if (!isValid)
        {
          result="You must enter an email";

        }
      }
      return result;
    }
}

Forgive your enemies - it messes with their heads


My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility


GeneralRe: WPF textbox email validation Pin
arkiboys28-Apr-11 0:36
arkiboys28-Apr-11 0:36 
GeneralRe: WPF textbox email validation Pin
Pete O'Hanlon28-Apr-11 0:46
mvePete O'Hanlon28-Apr-11 0:46 
QuestionHow to read data from listbox.Itemtemplate and pass it in xml format Pin
Rocky2327-Apr-11 20:58
Rocky2327-Apr-11 20:58 
AnswerRe: How to read data from listbox.Itemtemplate and pass it in xml format Pin
Abhinav S27-Apr-11 21:33
Abhinav S27-Apr-11 21:33 
GeneralRe: How to read data from listbox.Itemtemplate and pass it in xml format Pin
Rocky2327-Apr-11 21:50
Rocky2327-Apr-11 21:50 
GeneralRe: How to read data from listbox.Itemtemplate and pass it in xml format Pin
Pete O'Hanlon27-Apr-11 21:55
mvePete O'Hanlon27-Apr-11 21:55 
QuestionLine chart API Pin
anishkannan27-Apr-11 20:17
anishkannan27-Apr-11 20:17 
AnswerRe: Line chart API Pin
Abhinav S27-Apr-11 21:31
Abhinav S27-Apr-11 21:31 
AnswerRe: Line chart API Pin
Pete O'Hanlon27-Apr-11 22:07
mvePete O'Hanlon27-Apr-11 22:07 
QuestionAssembly Not Found Error In XAML [modified] Pin
Kevin Marois27-Apr-11 10:51
professionalKevin Marois27-Apr-11 10:51 
AnswerRe: Assembly Not Found Error In XAML Pin
Abhinav S27-Apr-11 18:37
Abhinav S27-Apr-11 18:37 
GeneralRe: Assembly Not Found Error In XAML Pin
Kevin Marois28-Apr-11 7:17
professionalKevin Marois28-Apr-11 7:17 
QuestionWPF namespaces reference [modified] Pin
devvvy26-Apr-11 23:19
devvvy26-Apr-11 23:19 
AnswerRe: WPF namespaces reference Pin
Jammer27-Apr-11 3:42
Jammer27-Apr-11 3:42 
GeneralRe: WPF namespaces reference Pin
devvvy27-Apr-11 21:08
devvvy27-Apr-11 21:08 
AnswerRe: WPF namespaces reference Pin
Ian Shlasko27-Apr-11 4:01
Ian Shlasko27-Apr-11 4:01 
GeneralRe: WPF namespaces reference Pin
SledgeHammer0127-Apr-11 6:51
SledgeHammer0127-Apr-11 6:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.