Validation In Xamarin Forms












0















I am learning Xamarin Forms and am attempting to add in validation. What I want to do is if a valid email address is not input, then I want to outline the field in red and display a hidden label.



I get this error:




System.NullReferenceException has been thrown




In my my XF_Login.iOS



This is my syntax:



    <ContentPage.Content>  
<StackLayout Orientation="Vertical" Padding="30" Spacing="40">
<BoxView HeightRequest="10"/>
<Frame BackgroundColor="#BF043055" HasShadow="False">
<StackLayout Orientation="Vertical" Spacing="10">
<Entry x:Name="Email" Text="{Binding Email}" Placeholder="Email"
PlaceholderColor="Red" HeightRequest="50"
TextColor="Black"/>
<Label x:Name="emailerror" Text="Error this is invalid format. Please udpate" TextColor="Red" />
</StackLayout>
</Frame>
<Button Command="{Binding SubmitCommand}" Text="Register" TextColor="White"
FontAttributes="Bold" FontSize="Large" HorizontalOptions="FillAndExpand"
BackgroundColor="#088da5" Clicked="OnRegisterTap" />
</StackLayout>
</ContentPage.Content>

//Constructor
public RegisterPage()
{
//Setting the label to invisible until needed
emailerror.IsVisible = false;

//Wiring up the event for the email check
Email.Completed += new EventHandler(Email_Completed);

InitializeComponent();
}

//Custom event
void Email_Completed(object sender, EventArgs e)
{
bool isEmail = Regex.IsMatch(Email.Text, @"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);

if (isEmail == false)
{
emailerror.IsVisible = true;
Email.BackgroundColor = Color.FromHex("#2c3e50");
Email.TextColor = Color.Red;
}
}
void OnRegisterTap(object sender, EventArgs e)
{
App.Current.MainPage = new NavigationPage(new AlreadyRegistered());
}
public partial class AlreadyRegistered : ContentPage
{
public AlreadyRegistered()
{
InitializeComponent();
}
}
//Error Method
namespace XF_Login.iOS
{
public class Application
{
// This is the main entry point of the application.
static void Main(string args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, "AppDelegate");
}
}
}









share|improve this question

























  • which line causes the exception? And where is the call to InitializeComponent in your constructor? Did you accidentally delete it?

    – Jason
    Nov 25 '18 at 2:17











  • @Jason - sorry that was a copy/paste error. This is the line of code that causes the error. On my button press event - App.Current.MainPage = new NavigationPage(new AlreadyRegistered());

    – Doctor Ford
    Nov 25 '18 at 2:21











  • what does the constructor of AlreadyRegistered look like? So far this appears to have nothing to do with your validation

    – Jason
    Nov 25 '18 at 2:25











  • @Jason - I'll edit my OP to include the constructor for AlreadyRegistered. That's interesting as if I comment out the Email_Completed method as well as the Email_Completed in my constructor the code executes fine

    – Doctor Ford
    Nov 25 '18 at 2:26











  • does this only happen when you click Register and the email field is empty?

    – Jason
    Nov 25 '18 at 2:32
















0















I am learning Xamarin Forms and am attempting to add in validation. What I want to do is if a valid email address is not input, then I want to outline the field in red and display a hidden label.



I get this error:




System.NullReferenceException has been thrown




In my my XF_Login.iOS



This is my syntax:



    <ContentPage.Content>  
<StackLayout Orientation="Vertical" Padding="30" Spacing="40">
<BoxView HeightRequest="10"/>
<Frame BackgroundColor="#BF043055" HasShadow="False">
<StackLayout Orientation="Vertical" Spacing="10">
<Entry x:Name="Email" Text="{Binding Email}" Placeholder="Email"
PlaceholderColor="Red" HeightRequest="50"
TextColor="Black"/>
<Label x:Name="emailerror" Text="Error this is invalid format. Please udpate" TextColor="Red" />
</StackLayout>
</Frame>
<Button Command="{Binding SubmitCommand}" Text="Register" TextColor="White"
FontAttributes="Bold" FontSize="Large" HorizontalOptions="FillAndExpand"
BackgroundColor="#088da5" Clicked="OnRegisterTap" />
</StackLayout>
</ContentPage.Content>

//Constructor
public RegisterPage()
{
//Setting the label to invisible until needed
emailerror.IsVisible = false;

//Wiring up the event for the email check
Email.Completed += new EventHandler(Email_Completed);

InitializeComponent();
}

//Custom event
void Email_Completed(object sender, EventArgs e)
{
bool isEmail = Regex.IsMatch(Email.Text, @"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);

if (isEmail == false)
{
emailerror.IsVisible = true;
Email.BackgroundColor = Color.FromHex("#2c3e50");
Email.TextColor = Color.Red;
}
}
void OnRegisterTap(object sender, EventArgs e)
{
App.Current.MainPage = new NavigationPage(new AlreadyRegistered());
}
public partial class AlreadyRegistered : ContentPage
{
public AlreadyRegistered()
{
InitializeComponent();
}
}
//Error Method
namespace XF_Login.iOS
{
public class Application
{
// This is the main entry point of the application.
static void Main(string args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, "AppDelegate");
}
}
}









share|improve this question

























  • which line causes the exception? And where is the call to InitializeComponent in your constructor? Did you accidentally delete it?

    – Jason
    Nov 25 '18 at 2:17











  • @Jason - sorry that was a copy/paste error. This is the line of code that causes the error. On my button press event - App.Current.MainPage = new NavigationPage(new AlreadyRegistered());

    – Doctor Ford
    Nov 25 '18 at 2:21











  • what does the constructor of AlreadyRegistered look like? So far this appears to have nothing to do with your validation

    – Jason
    Nov 25 '18 at 2:25











  • @Jason - I'll edit my OP to include the constructor for AlreadyRegistered. That's interesting as if I comment out the Email_Completed method as well as the Email_Completed in my constructor the code executes fine

    – Doctor Ford
    Nov 25 '18 at 2:26











  • does this only happen when you click Register and the email field is empty?

    – Jason
    Nov 25 '18 at 2:32














0












0








0








I am learning Xamarin Forms and am attempting to add in validation. What I want to do is if a valid email address is not input, then I want to outline the field in red and display a hidden label.



I get this error:




System.NullReferenceException has been thrown




In my my XF_Login.iOS



This is my syntax:



    <ContentPage.Content>  
<StackLayout Orientation="Vertical" Padding="30" Spacing="40">
<BoxView HeightRequest="10"/>
<Frame BackgroundColor="#BF043055" HasShadow="False">
<StackLayout Orientation="Vertical" Spacing="10">
<Entry x:Name="Email" Text="{Binding Email}" Placeholder="Email"
PlaceholderColor="Red" HeightRequest="50"
TextColor="Black"/>
<Label x:Name="emailerror" Text="Error this is invalid format. Please udpate" TextColor="Red" />
</StackLayout>
</Frame>
<Button Command="{Binding SubmitCommand}" Text="Register" TextColor="White"
FontAttributes="Bold" FontSize="Large" HorizontalOptions="FillAndExpand"
BackgroundColor="#088da5" Clicked="OnRegisterTap" />
</StackLayout>
</ContentPage.Content>

//Constructor
public RegisterPage()
{
//Setting the label to invisible until needed
emailerror.IsVisible = false;

//Wiring up the event for the email check
Email.Completed += new EventHandler(Email_Completed);

InitializeComponent();
}

//Custom event
void Email_Completed(object sender, EventArgs e)
{
bool isEmail = Regex.IsMatch(Email.Text, @"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);

if (isEmail == false)
{
emailerror.IsVisible = true;
Email.BackgroundColor = Color.FromHex("#2c3e50");
Email.TextColor = Color.Red;
}
}
void OnRegisterTap(object sender, EventArgs e)
{
App.Current.MainPage = new NavigationPage(new AlreadyRegistered());
}
public partial class AlreadyRegistered : ContentPage
{
public AlreadyRegistered()
{
InitializeComponent();
}
}
//Error Method
namespace XF_Login.iOS
{
public class Application
{
// This is the main entry point of the application.
static void Main(string args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, "AppDelegate");
}
}
}









share|improve this question
















I am learning Xamarin Forms and am attempting to add in validation. What I want to do is if a valid email address is not input, then I want to outline the field in red and display a hidden label.



I get this error:




System.NullReferenceException has been thrown




In my my XF_Login.iOS



This is my syntax:



    <ContentPage.Content>  
<StackLayout Orientation="Vertical" Padding="30" Spacing="40">
<BoxView HeightRequest="10"/>
<Frame BackgroundColor="#BF043055" HasShadow="False">
<StackLayout Orientation="Vertical" Spacing="10">
<Entry x:Name="Email" Text="{Binding Email}" Placeholder="Email"
PlaceholderColor="Red" HeightRequest="50"
TextColor="Black"/>
<Label x:Name="emailerror" Text="Error this is invalid format. Please udpate" TextColor="Red" />
</StackLayout>
</Frame>
<Button Command="{Binding SubmitCommand}" Text="Register" TextColor="White"
FontAttributes="Bold" FontSize="Large" HorizontalOptions="FillAndExpand"
BackgroundColor="#088da5" Clicked="OnRegisterTap" />
</StackLayout>
</ContentPage.Content>

//Constructor
public RegisterPage()
{
//Setting the label to invisible until needed
emailerror.IsVisible = false;

//Wiring up the event for the email check
Email.Completed += new EventHandler(Email_Completed);

InitializeComponent();
}

//Custom event
void Email_Completed(object sender, EventArgs e)
{
bool isEmail = Regex.IsMatch(Email.Text, @"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);

if (isEmail == false)
{
emailerror.IsVisible = true;
Email.BackgroundColor = Color.FromHex("#2c3e50");
Email.TextColor = Color.Red;
}
}
void OnRegisterTap(object sender, EventArgs e)
{
App.Current.MainPage = new NavigationPage(new AlreadyRegistered());
}
public partial class AlreadyRegistered : ContentPage
{
public AlreadyRegistered()
{
InitializeComponent();
}
}
//Error Method
namespace XF_Login.iOS
{
public class Application
{
// This is the main entry point of the application.
static void Main(string args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, "AppDelegate");
}
}
}






c# xamarin xamarin.forms xamarin.ios






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 2:27







Doctor Ford

















asked Nov 25 '18 at 1:57









Doctor FordDoctor Ford

638




638













  • which line causes the exception? And where is the call to InitializeComponent in your constructor? Did you accidentally delete it?

    – Jason
    Nov 25 '18 at 2:17











  • @Jason - sorry that was a copy/paste error. This is the line of code that causes the error. On my button press event - App.Current.MainPage = new NavigationPage(new AlreadyRegistered());

    – Doctor Ford
    Nov 25 '18 at 2:21











  • what does the constructor of AlreadyRegistered look like? So far this appears to have nothing to do with your validation

    – Jason
    Nov 25 '18 at 2:25











  • @Jason - I'll edit my OP to include the constructor for AlreadyRegistered. That's interesting as if I comment out the Email_Completed method as well as the Email_Completed in my constructor the code executes fine

    – Doctor Ford
    Nov 25 '18 at 2:26











  • does this only happen when you click Register and the email field is empty?

    – Jason
    Nov 25 '18 at 2:32



















  • which line causes the exception? And where is the call to InitializeComponent in your constructor? Did you accidentally delete it?

    – Jason
    Nov 25 '18 at 2:17











  • @Jason - sorry that was a copy/paste error. This is the line of code that causes the error. On my button press event - App.Current.MainPage = new NavigationPage(new AlreadyRegistered());

    – Doctor Ford
    Nov 25 '18 at 2:21











  • what does the constructor of AlreadyRegistered look like? So far this appears to have nothing to do with your validation

    – Jason
    Nov 25 '18 at 2:25











  • @Jason - I'll edit my OP to include the constructor for AlreadyRegistered. That's interesting as if I comment out the Email_Completed method as well as the Email_Completed in my constructor the code executes fine

    – Doctor Ford
    Nov 25 '18 at 2:26











  • does this only happen when you click Register and the email field is empty?

    – Jason
    Nov 25 '18 at 2:32

















which line causes the exception? And where is the call to InitializeComponent in your constructor? Did you accidentally delete it?

– Jason
Nov 25 '18 at 2:17





which line causes the exception? And where is the call to InitializeComponent in your constructor? Did you accidentally delete it?

– Jason
Nov 25 '18 at 2:17













@Jason - sorry that was a copy/paste error. This is the line of code that causes the error. On my button press event - App.Current.MainPage = new NavigationPage(new AlreadyRegistered());

– Doctor Ford
Nov 25 '18 at 2:21





@Jason - sorry that was a copy/paste error. This is the line of code that causes the error. On my button press event - App.Current.MainPage = new NavigationPage(new AlreadyRegistered());

– Doctor Ford
Nov 25 '18 at 2:21













what does the constructor of AlreadyRegistered look like? So far this appears to have nothing to do with your validation

– Jason
Nov 25 '18 at 2:25





what does the constructor of AlreadyRegistered look like? So far this appears to have nothing to do with your validation

– Jason
Nov 25 '18 at 2:25













@Jason - I'll edit my OP to include the constructor for AlreadyRegistered. That's interesting as if I comment out the Email_Completed method as well as the Email_Completed in my constructor the code executes fine

– Doctor Ford
Nov 25 '18 at 2:26





@Jason - I'll edit my OP to include the constructor for AlreadyRegistered. That's interesting as if I comment out the Email_Completed method as well as the Email_Completed in my constructor the code executes fine

– Doctor Ford
Nov 25 '18 at 2:26













does this only happen when you click Register and the email field is empty?

– Jason
Nov 25 '18 at 2:32





does this only happen when you click Register and the email field is empty?

– Jason
Nov 25 '18 at 2:32












1 Answer
1






active

oldest

votes


















0














First, in a XAML code behind you must call InitializeComponent in the constructor before you reference any XAML elements. InitializeComponent handles loading the XAML elements, if you do not call it the refs will be null.



Second, Completed only fires when Return is pressed on an Entry field. To detect when a user moves to another field, try using Unfocused instead.






share|improve this answer


























  • if I change my syntax to Email.Unfocus += Email_Unfocus; I get a compile error of can not assign to Email.Unfocus bc it is a method group

    – Doctor Ford
    Nov 25 '18 at 2:56











  • Should be Unfocused

    – Jason
    Nov 25 '18 at 2:59











  • That got it!! Sorry for my beginner mistakes

    – Doctor Ford
    Nov 25 '18 at 3:00











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464039%2fvalidation-in-xamarin-forms%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














First, in a XAML code behind you must call InitializeComponent in the constructor before you reference any XAML elements. InitializeComponent handles loading the XAML elements, if you do not call it the refs will be null.



Second, Completed only fires when Return is pressed on an Entry field. To detect when a user moves to another field, try using Unfocused instead.






share|improve this answer


























  • if I change my syntax to Email.Unfocus += Email_Unfocus; I get a compile error of can not assign to Email.Unfocus bc it is a method group

    – Doctor Ford
    Nov 25 '18 at 2:56











  • Should be Unfocused

    – Jason
    Nov 25 '18 at 2:59











  • That got it!! Sorry for my beginner mistakes

    – Doctor Ford
    Nov 25 '18 at 3:00
















0














First, in a XAML code behind you must call InitializeComponent in the constructor before you reference any XAML elements. InitializeComponent handles loading the XAML elements, if you do not call it the refs will be null.



Second, Completed only fires when Return is pressed on an Entry field. To detect when a user moves to another field, try using Unfocused instead.






share|improve this answer


























  • if I change my syntax to Email.Unfocus += Email_Unfocus; I get a compile error of can not assign to Email.Unfocus bc it is a method group

    – Doctor Ford
    Nov 25 '18 at 2:56











  • Should be Unfocused

    – Jason
    Nov 25 '18 at 2:59











  • That got it!! Sorry for my beginner mistakes

    – Doctor Ford
    Nov 25 '18 at 3:00














0












0








0







First, in a XAML code behind you must call InitializeComponent in the constructor before you reference any XAML elements. InitializeComponent handles loading the XAML elements, if you do not call it the refs will be null.



Second, Completed only fires when Return is pressed on an Entry field. To detect when a user moves to another field, try using Unfocused instead.






share|improve this answer















First, in a XAML code behind you must call InitializeComponent in the constructor before you reference any XAML elements. InitializeComponent handles loading the XAML elements, if you do not call it the refs will be null.



Second, Completed only fires when Return is pressed on an Entry field. To detect when a user moves to another field, try using Unfocused instead.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 2:58

























answered Nov 25 '18 at 2:54









JasonJason

51.9k1290118




51.9k1290118













  • if I change my syntax to Email.Unfocus += Email_Unfocus; I get a compile error of can not assign to Email.Unfocus bc it is a method group

    – Doctor Ford
    Nov 25 '18 at 2:56











  • Should be Unfocused

    – Jason
    Nov 25 '18 at 2:59











  • That got it!! Sorry for my beginner mistakes

    – Doctor Ford
    Nov 25 '18 at 3:00



















  • if I change my syntax to Email.Unfocus += Email_Unfocus; I get a compile error of can not assign to Email.Unfocus bc it is a method group

    – Doctor Ford
    Nov 25 '18 at 2:56











  • Should be Unfocused

    – Jason
    Nov 25 '18 at 2:59











  • That got it!! Sorry for my beginner mistakes

    – Doctor Ford
    Nov 25 '18 at 3:00

















if I change my syntax to Email.Unfocus += Email_Unfocus; I get a compile error of can not assign to Email.Unfocus bc it is a method group

– Doctor Ford
Nov 25 '18 at 2:56





if I change my syntax to Email.Unfocus += Email_Unfocus; I get a compile error of can not assign to Email.Unfocus bc it is a method group

– Doctor Ford
Nov 25 '18 at 2:56













Should be Unfocused

– Jason
Nov 25 '18 at 2:59





Should be Unfocused

– Jason
Nov 25 '18 at 2:59













That got it!! Sorry for my beginner mistakes

– Doctor Ford
Nov 25 '18 at 3:00





That got it!! Sorry for my beginner mistakes

– Doctor Ford
Nov 25 '18 at 3:00




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464039%2fvalidation-in-xamarin-forms%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python