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 / Forcing a pattern in t...
Power Apps
Answered

Forcing a pattern in textfield using regex

(0) ShareShare
ReportReport
Posted on by 172

I am trying to validate my text field with some regex pattern

 

here is the sample text 

ABC-D-2013/07/15-1115

or 

DEF-N-2012/07/15-2224, BOY-W-2018/05/16-4441

 

this is my code

 

If(
IsMatch(
e_url.Text,
"([a-zA-Z]+-[a-zA-Z]+-\d+\/\d+\/\d+-\d+(?:,\s*[a-zA-Z]+-[a-zA-Z]+-\d+\/\d+\/\d+-\d+)?)",
MatchOptions.Complete
),
Color.White,
Color.Red
)

I am trying to change the fill color to red if it doesn't match.

it is always red. 

is the code right?

 

Categories:
  • Chris-D Profile Picture
    1,246 on at

    Hi @KDAI, I think there's an error in your Regex. There's an open bracket at the start which doesn't seem to have a close bracket to match. 

     

    Try this Regex though, it might work better:

    ([a-zA-Z]+-[a-zA-Z]+-\d+\/\d+\/\d+-\d+(,\s)*)+

     

    This doesn't seem to validate the amount of letters/numbers though, any combination will validate as long as there is at least 1 letter/number. 

     

    This would be valid: QWERTY-UIOP-1/2/3-9999999999999999, BOY-W-2018/05/16-4441

  • KDAI Profile Picture
    172 on at

    Did work half way through, now i need the numbering part. 

    If(
    IsMatch(
    e_url.Text,
    "([a-zA-Z]+-[a-zA-Z]+-\d+\/\d+\/\d+-\d+(,\s)*)+",
    MatchOptions.Complete
    ),
    Set(
    varvisible,
    false
    ),
    Set(
    varvisible,
    true
    )
    )

     

    i added a label with text and set the visible value to it. 

  • Verified answer
    MudassarSZ365 Profile Picture
    591 on at

    Hi @Chris-D ,

    try this

    The adjusted regex pattern with additional parentheses around the whole pattern could be:

    "^([a-zA-Z]+-[a-zA-Z]-\d{4}\/\d{2}\/\d{2}-\d{4}(?:,\s*[a-zA-Z]+-[a-zA-Z]-\d{4}\/\d{2}\/\d{2}-\d{4})*)$"

    So the IsMatch function in Power Apps would be:

    If(
     IsMatch(
     e_url.Text,
     "^([a-zA-Z]+-[a-zA-Z]-\d{4}/\d{2}/\d{2}-\d{4}(?:,\\s*[a-zA-Z]+-[a-zA-Z]-\d{4}/\d{2}/\d{2}-\d{4})*)$",
     MatchOptions.Complete
     ),
     Color.White,
     Color.Red
    )
  • Chris-D Profile Picture
    1,246 on at

    Hi @KDAI, 

    I'm not sure what this means? 

    "now i need the numbering part"

  • KDAI Profile Picture
    172 on at

    that means this is the pattern

    ABC-D-2013/07/15-1115 

    with exact number of characters in between

  • Chris-D Profile Picture
    1,246 on at

    ok we can do this with a couple of things. 

     

    First I'm going to remove the 2 cases from the character matches. You should add IgnoreCase to your match options, it makes the Regex clearer to read. 

     

     

    ([A-Z]{3}-[A-Z]-\d{4}\/\d{2}\/\d{2}-\d{4}(,\s)*)+
    
    // Let's break it down
    (
     [A-Z]{3} - match A-Z, exactly 3 times
     -hyphen-
     [A-Z] - match A-Z but only a single character (removed the +)
     -hyphen-
     \d{4} - match a digit, exactly 4 times
     -hyphen-
     \d{2} - ... but twice
     -hyphen-
     \d{2} - ... but twice
     -hyphen-
     \d{4} - ... and 4 times again
     (
     , - a comma
     \s - single whitespace (could break, might be tab or newline but unlikely in your case)
     )* - * = match this bit 0 or more times - it may or may not be present
    )+ - match the whole thing at least once

     

    That should be pretty bombproof for you and I hope my explanation is clear. I should question though why you are validating your output? The content should be validated when it's input. 

     

  • KDAI Profile Picture
    172 on at

    I already did this with following 

     

        ^([a-zA-Z0-9]{3,4}-[a-zA-Z]-\d{4}\/\d{2}\/\d{2}-\d{4}(?:,\s*[a-zA-Z0-9]{3,4}-[a-zA-Z]-\d{4}\/\d{2}\/\d{2}-\d{4})*)$

     

    this is input validation on a text field. 

  • Chris-D Profile Picture
    1,246 on at

    Sorry my bad, for some reason I'd read your original post as a text label. 

     

    Please see my shortened version though, it's not good to duplicate the whole regex, and you shouldn't need the ^ and $ because it won't match a partial string anyway. 

  • KDAI Profile Picture
    172 on at

    you mean this 

    ([A-Z]{3}-[A-Z]-\d{4}\/\d{2}\/\d{2}-\d{4}(,\s)*)+

    it is not case sensitive, is it? does it allow multiple entries with same pattern text separated by ,  it's a text field 255 character long. 

  • Chris-D Profile Picture
    1,246 on at

    Yes it's case sensitive, but you can add MatchOptions.IgnoreCase to the IsMatch() function's options. It's my preference but I think it makes the Regex easier to read. Unless your input will always be upper case in which case you don't need to worry about that. 

     

    It should allow multiple entries, separated by ", ". That is, a comma and a space. Nothing else. There must be a comma and there must be a space. Just one space. 

     

    ChrisD_0-1699463077675.png

    ChrisD_1-1699463136563.png

    Regular Expressions can be unpredictable though. You should thoroughly test this in any way you can think of that your users might enter slightly different data. 

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

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 224 Super User 2026 Season 2

#2
WarrenBelz Profile Picture

WarrenBelz 189 Most Valuable Professional

#3
Mohsin Ali Profile Picture

Mohsin Ali 126

Last 30 days Overall leaderboard