55using Microsoft . UI . Xaml . Input ;
66using Microsoft . UI . Xaml . Media ;
77using Microsoft . UI . Xaml . Navigation ;
8+ using Syncfusion . UI . Xaml . Editors ;
89using System ;
910using System . Collections . Generic ;
1011using System . IO ;
1112using System . Linq ;
1213using System . Runtime . InteropServices . WindowsRuntime ;
14+ using System . Text . RegularExpressions ;
1315using Windows . Foundation ;
1416using Windows . Foundation . Collections ;
17+ using Windows . UI . Popups ;
1518
1619// To learn more about WinUI, the WinUI project structure,
1720// and more about our project templates, see: http://aka.ms/winui-project-info.
@@ -27,5 +30,43 @@ public MainWindow()
2730 {
2831 this . InitializeComponent ( ) ;
2932 }
33+
34+ private async void OnInputSubmitted ( object sender , ComboBoxInputSubmittedEventArgs e )
35+ {
36+ var emailString = e . Text ;
37+ bool isEmail = Regex . IsMatch ( emailString , @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z" , RegexOptions . IgnoreCase ) ;
38+
39+ // If given input is a valid email format
40+ if ( isEmail )
41+ {
42+ // Create new person for selection
43+ Person newPerson = new Person ( ) ;
44+ newPerson . EmailID = emailString ;
45+ int atIndex = emailString . LastIndexOf ( '@' ) ;
46+ int dotIndex = emailString . IndexOf ( '.' ) ;
47+ string name = emailString . Substring ( 0 , atIndex ) ;
48+ if ( name . Contains ( '.' ) )
49+ {
50+ newPerson . FirstName = name . Substring ( 0 , dotIndex ) ;
51+ newPerson . LastName = name . Substring ( dotIndex + 1 ) ;
52+ }
53+ else
54+ {
55+ newPerson . FirstName = name ;
56+ }
57+ e . Item = newPerson ;
58+ }
59+
60+ // If email is id invalid, show error dialog.
61+ else
62+ {
63+ var dialog = new ContentDialog ( ) ;
64+ dialog . CloseButtonText = "Close" ;
65+ dialog . Content = "Invalid email id!" ;
66+ dialog . Title = "Error" ;
67+ dialog . XamlRoot = this . Content . XamlRoot ;
68+ await dialog . ShowAsync ( ) ;
69+ }
70+ }
3071 }
3172}
0 commit comments