web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / E-Mail validation on f...
Power Apps
Answered

E-Mail validation on field that is not always visible

(0) ShareShare
ReportReport
Posted on by 26

have a field for e-mail, that is only visible if "Yes" is chosen in the radio button on the left. There is an e-mail validation, so the e-mail field can't be blank, and requires an @ sign in order to be validated. If the e-mail field is not visible (When "Key required" = No), the validation is still in place, so the form cannot be submitted, as the field is empty. How can i get around this?

 

Udklip.PNG

 

Categories:
I have the same question (0)
  • LaurensM Profile Picture
    12,516 Moderator on at

    Hi @VBL,

     

    Make sure the email is not a required field in your table or SP list otherwise the adjustment below will not work. If it is, make it optional and refresh your datasource in your app.

     

    To make the field dynamically required, only when "Yes" is chosen, please use following code in the Required property of the email DataCard (not the textinput but the DataCard that contains the text input):

    //Please adjust it to the correct DataCardValue (the yes no toggle)
    YesNoDataCardValue.Value //Returns true if yes is selected

     

    If this solves your question, would you be so kind as to accept it as a solution.

    Thanks!

  • VBL Profile Picture
    26 on at

    Hi @LaurensM 

     

    Thanks for your quick reply

     

    I probably should have specified - the app is not built with forms, but with regular labels and input fields - is it correct that these do not have the "Required" property?

  • LaurensM Profile Picture
    12,516 Moderator on at

    Hi @VBL,

     

    Indeed only form datacards have a required property, in this case you will have to add a condition to your email checks. I expect that these checks are performed right before patching the data to the source? An If() statement should do the trick:

     

    If(
     Toggle1.Value, //returns true when yes is selected
     //Email validation & Patch with email field included,
     //Patch without email field and no email validation
    )

    Feel free to provide your specific code, so we can go over it together. 🙂

     

    I hope this helps!

  • VBL Profile Picture
    26 on at

    Hi @LaurensM - thanks, i appreciate your help 🙂

     

    That's what i thought - i am very new at PowerApps, so there is probably a better way to go about this, but currently a button contains the following:

     

    If(IsMatch(EmailInput.Text;
    "^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+.([a-zA-Z0-9]{2,4})+$");

     

    If(!IsBlank(Patch('External Visitor List 2'; Defaults('External Visitor List 2');{'First name':FirstNameInput.Text;'Last Name':LastNameInput.Text;Company:CompanyInput.Text;Host:HostNameInput.Text;'Start date':DatePicker1.SelectedDate;'End date':If(DatePicker2.Visible;DatePicker2.SelectedDate;Blank());'Key required?':Radio2.Selected.Value;'E-Mail':EmailInput.Text}));
    UpdateContext({locReset:true});;
    UpdateContext({locReset:false});;
    Navigate(CompletionScreen;ScreenTransition.Fade));
    Notify("Email not valid"; NotificationType.Error))

     

    If it's easier to make sense of, i've added a screenshot of the code as well.

    Code snippet.PNG

     

  • LaurensM Profile Picture
    12,516 Moderator on at

    Hi @VBL,

     

    If you hadn't mentioned it, I would not have known that you were new to Power Apps - the code looks good. 😉

     

    I made some small adjustments:

    • Adjusted the errorlogging: If(Errors()) instead of !IsBlank()
    • Added ToggleName.Value as an Or parameter to your email validation
    • The Blank() value in the End Date If statement can be left out (only provide a value if it is visible)

     

    If(
     //Either the Toggle = false or the email has to be valid (adjust the Toggle name to the correct one)
     !ToggleName.Value || IsMatch(EmailInput.Text; "^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+.([a-zA-Z0-9]{2,4})+$");
     Patch(
     'External Visitor List 2'; 
     Defaults('External Visitor List 2');
     {
     'First name':FirstNameInput.Text;
     'Last Name':LastNameInput.Text;
     Company:CompanyInput.Text;
     Host:HostNameInput.Text;
     'Start date':DatePicker1.SelectedDate;
     'End date':If(DatePicker2.Visible; DatePicker2.SelectedDate);
     'Key required?':Radio2.Selected.Value;
     'E-Mail':EmailInput.Text
     }
     );;
     //Check for errors on our Patch() statement
     If(
     IsEmpty(Errors('External Visitor List 2'));
     //In case no errors were found, execute the following functions
     UpdateContext({locReset:true});;
     UpdateContext({locReset:false});;
     Navigate(CompletionScreen;ScreenTransition.Fade)
     );
     //In case the email was invalid, notify the user
     Notify("Email not valid"; NotificationType.Error)
    )

     

     

    If this solves your question, would you be so kind as to accept it as a solution.

    Thanks!

  • LaurensM Profile Picture
    12,516 Moderator on at

    @VBL as a note: I made a small mistake in my code above initially.

    (I now added the ! before ToggleName - the code snippet is up to date)

  • VBL Profile Picture
    26 on at

    Edit: I didn't see your reply before sending this one, so i will test the updated code 🙂 

    Edit2: @LaurensM i tested the updated code, with the snippet below, and now it seems to let me through - however, it seems to skip the E-mail validation, as i can now submit the form with an invalid e-mail (no @-sign) - any suggestions?

     

    Hi - well, doesn't seem like i'm completely lost then 🙂

     

    However, i am not quite sure how to get it to work, as it still brings up the error message and doesn't allow me to proceed, as long as the e-mail input field remains empty.

     

    I'm unsure how the toggle in the first part needs to be setup, but i'm willing to bet that this is not exactly what you had in mind (tried both with and without the = "Yes", as is shown below):

     

     

    If(
     //Either the Toggle = false or the email has to be valid (adjust the Toggle name to the correct one)
     !Radio2.Selected.Value || IsMatch(EmailInput.Text; "^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+.([a-zA-Z0-9]{2,4})+$");
     Patch(
     'External Visitor List 2'; 
     Defaults('External Visitor List 2');
     {
     'First name':FirstNameInput.Text;
     'Last Name':LastNameInput.Text;
     Company:CompanyInput.Text;
     Host:HostNameInput.Text;
     'Start date':DatePicker1.SelectedDate;
     'End date':If(DatePicker2.Visible; DatePicker2.SelectedDate);
     'Key required?':Radio2.Selected.Value;
     'E-Mail':EmailInput.Text
     }
     );;
     //Check for errors on our Patch() statement
     If(
     IsEmpty(Errors('External Visitor List 2'));
     //In case no errors were found, execute the following functions
     UpdateContext({locReset:true});;
     UpdateContext({locReset:false});;
     Navigate(CompletionScreen;ScreenTransition.Fade)
     );
     //In case the email was invalid, notify the user
     Notify("Email not valid"; NotificationType.Error)
    )

     

     

    Also, do i need to change anything in the properties of the input fields themselves (other than what is already setup for toggling visibility of fields based on prior input), or should it all be able to work from the button itself, without further changes?

  • LaurensM Profile Picture
    12,516 Moderator on at

    @VBL I should pay more attention to the actual controls in screenshots - it indeed clearly is a radio button.

    Please try the following updated code:

     

    If(
     //Either the radio button = No or the email has to be valid (adjust the Toggle name to the correct one)
     Radio2.Selected.Value = "No" || IsMatch(EmailInput.Text; "^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+.([a-zA-Z0-9]{2,4})+$");
     Patch(
     'External Visitor List 2'; 
     Defaults('External Visitor List 2');
     {
     'First name':FirstNameInput.Text;
     'Last Name':LastNameInput.Text;
     Company:CompanyInput.Text;
     Host:HostNameInput.Text;
     'Start date':DatePicker1.SelectedDate;
     'End date':If(DatePicker2.Visible; DatePicker2.SelectedDate);
     'Key required?':Radio2.Selected.Value;
     'E-Mail':EmailInput.Text
     }
     );;
     //Check for errors on our Patch() statement
     If(
     IsEmpty(Errors('External Visitor List 2'));
     //In case no errors were found, execute the following functions
     UpdateContext({locReset:true});;
     UpdateContext({locReset:false});;
     Navigate(CompletionScreen;ScreenTransition.Fade)
     );
     //In case the email was invalid, notify the user
     Notify("Email not valid"; NotificationType.Error)
    )
  • VBL Profile Picture
    26 on at

    Hi @LaurensM 

     

    Just got back to testing - the new code doesn't seem to work, as it still checks for e-mail validation while the field is invisible.

     

    Any chance it could be dependant on other fields, the way that the "Visible" property is set up etc.? I'm kinda lost at to where to check for other factors playing into the validation still going off.

  • LaurensM Profile Picture
    12,516 Moderator on at

    Hi @VBL, with the Radio2.Selected.Value = "No"  I would not expect the code to throw your 'Email not valid' error.

     

    Are you receiving your custom notification or did you implement validation on your column (in SP) - or did you set the field to required?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 326 Most Valuable Professional

#2
11manish Profile Picture

11manish 168

#3
sannavajjala87 Profile Picture

sannavajjala87 75 Super User 2026 Season 1

Last 30 days Overall leaderboard