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 / Using IF and VAR to en...
Power Apps
Suggested Answer

Using IF and VAR to ensure valid phone number.

(1) ShareShare
ReportReport
Posted on by 483
Criteria:
1 - I want to ensure users are entering the phone number as (333) 333-3333. 
2 - If the phone number is incorrect, I want to change the COLOR and FILL values and I want to make the Clear_Phone button VISIBLE.  
3 - I don't want to see these changes as the user is entering the number, it should change after the last digit is entered and/or the field loses focus. 
 
I'm using OnChange here for the first time. It seems to be working but I'm not sure if the web designer / my mouse cursor is reflecting the real-world mobile app. 
 
I think this code (below) for the text input is working but is there a more efficient way to do this? Can I set all this code in one place and tell the elements (fill, color, visible) what to do? 
 
My main question:
I'm having issues with setting VISIBLE for Clear_Phone. I'm not sure where to place this value or if I should be setting a variable or what.  
 
I've tried 'if FILL is red and ONCHANGE is true, then Visible=true' but that's not working. It's always visible after one character is entered into the text field. I've tried setting a variable within an ISMATCH else statement but that's not resolving. 
 

TEXT INPUT: ContactPhone 
 
//ONCHANGE
Set (varContactPhone,true)
 
//FILL COLOR
If(varContactPhone,
If(IsBlank
(ContactPhone.Text),RGBA(252, 251, 237, 1),
IsMatch
(ContactPhone.Text, Match.LeftParen & Match.Digit & Match.Digit & Match.Digit & Match.RightParen & Match.Space & Match.Digit & Match.Digit & Match.Digit & Match.Hyphen & Match.Digit & Match.Digit & Match.Digit & Match.Digit),
RGBA(252, 251, 237, 1),
Color.Red)
,RGBA(252, 251, 237, 1))
 
//TEXT COLOR
If(varContactPhone,
If(IsBlank
(ContactPhone.Text),Color.Black,
IsMatch
(ContactPhone.Text, Match.LeftParen & Match.Digit & Match.Digit & Match.Digit & Match.RightParen & Match.Space & Match.Digit & Match.Digit & Match.Digit & Match.Hyphen & Match.Digit & Match.Digit & Match.Digit & Match.Digit),
Color.Black,
Color.Yellow)
,Color.Black)
Categories:
I have the same question (0)
  • Suggested answer
    Michael E. Gernaey Profile Picture
    53,963 Moderator on at
    Hi,
     
    Here is what you can do so that everything acts how you want.

    0. Remove the Variable from the OnChange you don't need it
    1. Add a label to the screen
    2. In the Labels Fill property, put your Code there. If the value is not valid yet keep the fille Color.Red when its valid make it Color.Green
     
    Now in any other control on the screen just say
     
    If(LabelName.Fill = Color.Red, DisplayMode.Disabled, DisplayMode.Enabled)
     
    In a Visible property you only need to say
     
    LabelName.Fill = Color.Green
    If its green then it will return true for visible and if its not it will return false for invisible
     
    You do not need to duplicate the code all over.
     
     
     
     
     
  • anthonys123 Profile Picture
    483 on at
    @FLMike You lost me here: 
     
    Now in any other control on the screen just say
     
    If(LabelName.Fill = Color.Red, DisplayMode.Disabled, DisplayMode.Enabled)
     
     
    What does 'any other control on the screen' mean?

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 316 Most Valuable Professional

#2
11manish Profile Picture

11manish 242

#3
Valantis Profile Picture

Valantis 198

Last 30 days Overall leaderboard