Home > Net >  How to ensure no additional text is added after an email address
How to ensure no additional text is added after an email address

Time:01-15

I have written a Powershell GUI that works with Exchange Online. I am trying to formulate an If statement to only allow my companies email addresses. I can get it to ensure that @mywebsite.com is required in the field, however this only half fixes my issue as if the user adds additional text after the email address, it still passes the If statement as it's detected that it contains @mywebsite.com. How can I formulate the statement so that if there's any additional text after @mywebsite.com, it will trigger the rule?

I know this can be achieved by adding "@mywebsite.com" "a" and do the same for each letter in the alphabet but I'm hoping there's a simpler solution.

CodePudding user response:

You can achieve this by doing a regex (regular expression) comparison that checks the end of the input string. In this case @mywebsite.com. You enforce to check the end of the string by using the $ character.

"[email protected]" -match "@mywebsite.com$"

Outputs: $True

"[email protected] Hey" -match "@mywebsite.com$"

Outputs: $False

  • Related