How to validate data using IDataErrorInfo in WPF?


Isn't it fun,

to instantly listen to a blog on the go? PLAY !

 
 

validate-data

In this blog, we will be going to discuss how to validate data using IDataErrorInfo in WPF. If you implement the validated data using IDataErrorInfo using WPF then firstly you must have basic knowledge about the WPF.

What is WPF?

WPF’s full form is Windows Presentation Foundation (WPF). WPF language is useful for creating or making desktop client applications. The WPF development platform supports a large set of applications like features, applications models, resources, control, graphics, data binding, document, and security.

WPF language is a part of the .Net Framework. If you have implemented the code in the .Net framework or Asp.Net then you can easily familiar with programming experience. WPF language is used to XAML (Extensible Application Markup Language) to create the UI.

Now, we have explained how the data validation in WPF using IDataErrorInfo?

Why Data validation is important in the WPF?

Data validation is important in the WPF because if the user enters some data in the form then this data is not valid and this time you have to validate the data. Using this interface, you can easily display the validation and also implement the automatic validation.


Example:

Please follow these steps, how to data validate using IDataErrorInfo in WPF?

Step 1: First create a new WPF Project using the .Net Framework and assign the project location and project name.

Step 2: After creating the project you have to create 3 folders and assign the names View, ViewModel, and Model.

Step 3: After creating the folders you have to create the first view page in the view folder and assign the name like StudentFormViewPage.xaml.

FIle Name: StudentForm.xaml Code:

    
        
            
                
                
                
                    
                
            
        
    
    
        
            
            
        
        
            
                
            
        
        
            
                
            
        
        
            
                
            
        
        
            
                
            
        
        

Step 4: After creating the design you have created a Datacontext object for binding the ViewModel class. This file is already created inside the xaml file.

File Name: StudentForm.xaml.cs Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ValidationDemoWPF.ViewModel;

namespace ValidationDemoWPF.View
{
    
    public partial class StudentForm : Window
    {
        public StudentForm()
        {
            InitializeComponent();
            this.DataContext = new ValidationViewModel();
        }
    }
}

Step 5: Now, you have to create the ViewModel class in the ViewModel folder and assign the name like ValidationViewModel.cs.

File Name: ValidationViewModel.cs Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ValidationDemoWPF.ViewModel
{
    public class ValidationViewModel: IDataErrorInfo
    {
        public int Stu_id { get; set; }
        public string FName { get; set; }
        public string LName { get; set; }
        public int Old_age { get; set; }
        public string State_Name { get; set; }
        public DateTime? DateofBirth { get; set; }
        public string EmailID { get; set; }
        public string Error
        {
            get
            {
                return this[string.Empty];
            }
        }
        public string this[string cuurectname]
        {
            get
            {
                string result1 = null;
                if (cuurectname == "FName")
                {
                    if (string.IsNullOrEmpty(FName))
                    {
                        result1 = "First Name. can not be empty.";
                        return result1;
                    }
                    String str = @"!|@|#|\$|%|\?|\>|\<|\*";
                    if (Regex.IsMatch(FName, str))
                    {
                        result1 = "Special charectors are not allowed hear.";
                        return result1;
                    }
                }
                if (cuurectname == "LName")
                {
                    if (string.IsNullOrEmpty(LName))
                    {
                        result1 = "Please enter the last name.";
                        return result1;
                    }
                }
                if (cuurectname == "Old_age")
                {
                    if (Old_age < 1 || Old_age > 70)
                    {
                        result1 = "Enter Age between 1 to 70.";
                        return result1;
                    }
                }
                if (cuurectname == "State_Name")
                {
                    if (string.IsNullOrEmpty(State_Name))
                    {
                        result1 = "State Cannot be Empty.";
                        return result1;
                    }
                }
                if (cuurectname == "DateofBirth")
                {
                    if (DateofBirth < DateTime.Parse("01-01-1987") || DateofBirth > DateTime.Now)
                    {
                     result1 = "Add DateofBirth between from 01-01-1987 to till date";
                        return result1;
                    }
                }
                if (cuurectname == "EmailID")
                {
                    string str1 = @"^([\w]+\.+[\w]+@[\w]+\.[a-zA-Z]{3})$";
                    string str2 = @"^[\w]+@+[\w]+\.[a-zA-Z]{3}$";
                    if (EmailID == null)
                    {
                        result1 = "Email cannot be empty";
              return result1;
                    }
                   if (Regex.IsMatch(EmailID, str1) || Regex.IsMatch(EmailID, str2))
                    {
                        return null;
                    }
                    else
                    {
                        result1 = "Invalid Email address(Format:manjushet@sourceedge.com)";

                        return result1;
                    }
                }
                return null;
            }
        }
    }
}

Step 6: Now you have to assign the first page in the App.xaml file using StartupUri.

File Name: App.xaml Code:

    
         
    

Step 7: Lastly, Run your project.

Figure: Output of the project

Conclusion

In this blog, we have explained how to validate data using IDataErrorInfo in WPF. If the user enters any data in whatever form at that time first you have to check if the inserted data is valid or not. If not then you have shown the message.

validate-data

In this blog, we will be going to discuss how to validate data using IDataErrorInfo in WPF. If you implement the validated data using IDataErrorInfo using WPF then firstly you must have basic knowledge about the WPF.

What is WPF?

WPF’s full form is Windows Presentation Foundation (WPF). WPF language is useful for creating or making desktop client applications. The WPF development platform supports a large set of applications like features, applications models, resources, control, graphics, data binding, document, and security.

WPF language is a part of the .Net Framework. If you have implemented the code in the .Net framework or Asp.Net then you can easily familiar with programming experience. WPF language is used to XAML (Extensible Application Markup Language) to create the UI.

Now, we have explained how the data validation in WPF using IDataErrorInfo?

Why Data validation is important in the WPF?

Data validation is important in the WPF because if the user enters some data in the form then this data is not valid and this time you have to validate the data. Using this interface, you can easily display the validation and also implement the automatic validation.


Example:

Please follow these steps, how to data validate using IDataErrorInfo in WPF?

Step 1: First create a new WPF Project using the .Net Framework and assign the project location and project name.

Step 2: After creating the project you have to create 3 folders and assign the names View, ViewModel, and Model.

Step 3: After creating the folders you have to create the first view page in the view folder and assign the name like StudentFormViewPage.xaml.

FIle Name: StudentForm.xaml Code:

    
        
            
                
                
                
                    
                
            
        
    
    
        
            
            
        
        
            
                
            
        
        
            
                
            
        
        
            
                
            
        
        
            
                
            
        
        

Step 4: After creating the design you have created a Datacontext object for binding the ViewModel class. This file is already created inside the xaml file.

File Name: StudentForm.xaml.cs Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ValidationDemoWPF.ViewModel;

namespace ValidationDemoWPF.View
{
    
    public partial class StudentForm : Window
    {
        public StudentForm()
        {
            InitializeComponent();
            this.DataContext = new ValidationViewModel();
        }
    }
}

Step 5: Now, you have to create the ViewModel class in the ViewModel folder and assign the name like ValidationViewModel.cs.

File Name: ValidationViewModel.cs Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ValidationDemoWPF.ViewModel
{
    public class ValidationViewModel: IDataErrorInfo
    {
        public int Stu_id { get; set; }
        public string FName { get; set; }
        public string LName { get; set; }
        public int Old_age { get; set; }
        public string State_Name { get; set; }
        public DateTime? DateofBirth { get; set; }
        public string EmailID { get; set; }
        public string Error
        {
            get
            {
                return this[string.Empty];
            }
        }
        public string this[string cuurectname]
        {
            get
            {
                string result1 = null;
                if (cuurectname == "FName")
                {
                    if (string.IsNullOrEmpty(FName))
                    {
                        result1 = "First Name. can not be empty.";
                        return result1;
                    }
                    String str = @"!|@|#|\$|%|\?|\>|\<|\*";
                    if (Regex.IsMatch(FName, str))
                    {
                        result1 = "Special charectors are not allowed hear.";
                        return result1;
                    }
                }
                if (cuurectname == "LName")
                {
                    if (string.IsNullOrEmpty(LName))
                    {
                        result1 = "Please enter the last name.";
                        return result1;
                    }
                }
                if (cuurectname == "Old_age")
                {
                    if (Old_age < 1 || Old_age > 70)
                    {
                        result1 = "Enter Age between 1 to 70.";
                        return result1;
                    }
                }
                if (cuurectname == "State_Name")
                {
                    if (string.IsNullOrEmpty(State_Name))
                    {
                        result1 = "State Cannot be Empty.";
                        return result1;
                    }
                }
                if (cuurectname == "DateofBirth")
                {
                    if (DateofBirth < DateTime.Parse("01-01-1987") || DateofBirth > DateTime.Now)
                    {
                     result1 = "Add DateofBirth between from 01-01-1987 to till date";
                        return result1;
                    }
                }
                if (cuurectname == "EmailID")
                {
                    string str1 = @"^([\w]+\.+[\w]+@[\w]+\.[a-zA-Z]{3})$";
                    string str2 = @"^[\w]+@+[\w]+\.[a-zA-Z]{3}$";
                    if (EmailID == null)
                    {
                        result1 = "Email cannot be empty";
              return result1;
                    }
                   if (Regex.IsMatch(EmailID, str1) || Regex.IsMatch(EmailID, str2))
                    {
                        return null;
                    }
                    else
                    {
                        result1 = "Invalid Email address(Format:manjushet@sourceedge.com)";

                        return result1;
                    }
                }
                return null;
            }
        }
    }
}

Step 6: Now you have to assign the first page in the App.xaml file using StartupUri.

File Name: App.xaml Code:

    
         
    

Step 7: Lastly, Run your project.

Figure: Output of the project

Conclusion

In this blog, we have explained how to validate data using IDataErrorInfo in WPF. If the user enters any data in whatever form at that time first you have to check if the inserted data is valid or not. If not then you have shown the message.