Skip to main content
Code Review

Return to Question

edited title
Link
janos
  • 113k
  • 15
  • 154
  • 396

What is best way for if statement to check validation instead of string? Validating user input in C# coming from XAML controls

deleted 65 characters in body
Source Link

I have page where I am inserting some data into Entry fields. For each Entry I have written in XAML different controls with parameters. One of them is Formated parameter where I am giving a string which I am checking on C# logic code. The problem which I figure out that some one could insert wrong string or string but for example with lower case. How can I improve this code for any wrong input scenarios ?

This is my xaml code:

<ContentPage.Content>
 <!--RegularExpresion="^[A-Za-z-ĄąČčĘęĖėĮįŠšŲųŪū ]+$"-->
 
 <StackLayout Padding="7,7,7,7" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Spacing="0">
 <cv:LabelEntryView x:Name="Name" LabelText="some text 4" ColorBg="White" MaxLength="20" Placeholder="some text 4" Formated="NameValidation" Keyboard="Text" >
 </cv:LabelEntryView>
 
 <cv:LabelEntryView x:Name="CardNr" LabelText="some text 3" ColorBg="White" MaxLength="20" Placeholder="some text 3" Formated="CardNrValidation" Keyboard="Numeric" >
 </cv:LabelEntryView>
 
 <cv:LabelEntryView x:Name="ExpDate" LabelText="some text 2" ColorBg="White" MaxLength="5" Placeholder="some text 2" Formated="ExpDate" Keyboard="Numeric" >
 </cv:LabelEntryView>
 
 <cv:LabelEntryView x:Name="SecurNr" LabelText="some text" ColorBg="White" MaxLength="3" Placeholder="some text" Keyboard="Numeric" >
 </cv:LabelEntryView>
 </StackLayout>
 </ContentPage.Content>

and here is my C# code with if statement checking string:

private void Entry_TextChanged(object sender, TextChangedEventArgs e)
 {
 if (Formated == "NameValidation")
 {
 Entry ent = (Entry)sender;
 ToUpper(ent);
 }
 else if (Formated == "CardNrValidation")
 {
 Entry ent = (Entry)sender;
 CardNumberValidation(ent);
 }
 else if (Formated == "ExpDate")
 {
 Entry ent = (Entry)sender;
 ExpDate(ent,e);
 }
}

Maybe I have to assign type for each if check? If yes, how can I do it? Thank you for your reviews.

I have page where I am inserting some data into Entry fields. For each Entry I have written in XAML different controls with parameters. One of them is Formated parameter where I am giving a string which I am checking on C# logic code. The problem which I figure out that some one could insert wrong string or string but for example with lower case. How can I improve this code for any wrong input scenarios ?

This is my xaml code:

<ContentPage.Content>
 <!--RegularExpresion="^[A-Za-z-ĄąČčĘęĖėĮįŠšŲųŪū ]+$"-->
 
 <StackLayout Padding="7,7,7,7" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Spacing="0">
 <cv:LabelEntryView x:Name="Name" LabelText="some text 4" ColorBg="White" MaxLength="20" Placeholder="some text 4" Formated="NameValidation" Keyboard="Text" >
 </cv:LabelEntryView>
 
 <cv:LabelEntryView x:Name="CardNr" LabelText="some text 3" ColorBg="White" MaxLength="20" Placeholder="some text 3" Formated="CardNrValidation" Keyboard="Numeric" >
 </cv:LabelEntryView>
 
 <cv:LabelEntryView x:Name="ExpDate" LabelText="some text 2" ColorBg="White" MaxLength="5" Placeholder="some text 2" Formated="ExpDate" Keyboard="Numeric" >
 </cv:LabelEntryView>
 
 <cv:LabelEntryView x:Name="SecurNr" LabelText="some text" ColorBg="White" MaxLength="3" Placeholder="some text" Keyboard="Numeric" >
 </cv:LabelEntryView>
 </StackLayout>
 </ContentPage.Content>

and here is my C# code with if statement checking string:

private void Entry_TextChanged(object sender, TextChangedEventArgs e)
 {
 if (Formated == "NameValidation")
 {
 Entry ent = (Entry)sender;
 ToUpper(ent);
 }
 else if (Formated == "CardNrValidation")
 {
 Entry ent = (Entry)sender;
 CardNumberValidation(ent);
 }
 else if (Formated == "ExpDate")
 {
 Entry ent = (Entry)sender;
 ExpDate(ent,e);
 }
}

Maybe I have to assign type for each if check? If yes, how can I do it? Thank you for your reviews.

I have page where I am inserting some data into Entry fields. For each Entry I have written in XAML different controls with parameters. One of them is Formated parameter where I am giving a string which I am checking on C# logic code. The problem which I figure out that some one could insert wrong string or string but for example with lower case. How can I improve this code for any wrong input scenarios ?

This is my xaml code:

<ContentPage.Content>
 
 <StackLayout Padding="7,7,7,7" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Spacing="0">
 <cv:LabelEntryView x:Name="Name" LabelText="some text 4" ColorBg="White" MaxLength="20" Placeholder="some text 4" Formated="NameValidation" Keyboard="Text" >
 </cv:LabelEntryView>
 
 <cv:LabelEntryView x:Name="CardNr" LabelText="some text 3" ColorBg="White" MaxLength="20" Placeholder="some text 3" Formated="CardNrValidation" Keyboard="Numeric" >
 </cv:LabelEntryView>
 
 <cv:LabelEntryView x:Name="ExpDate" LabelText="some text 2" ColorBg="White" MaxLength="5" Placeholder="some text 2" Formated="ExpDate" Keyboard="Numeric" >
 </cv:LabelEntryView>
 
 <cv:LabelEntryView x:Name="SecurNr" LabelText="some text" ColorBg="White" MaxLength="3" Placeholder="some text" Keyboard="Numeric" >
 </cv:LabelEntryView>
 </StackLayout>
 </ContentPage.Content>

and here is my C# code with if statement checking string:

private void Entry_TextChanged(object sender, TextChangedEventArgs e)
 {
 if (Formated == "NameValidation")
 {
 Entry ent = (Entry)sender;
 ToUpper(ent);
 }
 else if (Formated == "CardNrValidation")
 {
 Entry ent = (Entry)sender;
 CardNumberValidation(ent);
 }
 else if (Formated == "ExpDate")
 {
 Entry ent = (Entry)sender;
 ExpDate(ent,e);
 }
}

Maybe I have to assign type for each if check? If yes, how can I do it? Thank you for your reviews.

edited title
Link
BCdotWEB
  • 11.4k
  • 2
  • 28
  • 45

C# What is best way for if statement to check validation instead of string?

edited tags
Link
Loading
Source Link
Loading
default

AltStyle によって変換されたページ (->オリジナル) /