Password complexity condition [duplicate] - c#

This question already has answers here:
Regular expression to enforce complex passwords, matching 3 out of 4 rules
(4 answers)
Closed 8 years ago.
I have following requirement in my application. I need to maintain password complexity in my application as mention below.
password length is minimum 8 characters, a capital letter, a special character apart from # and numeric values which are not in sequence
Can anyone help me in getting regular expression for the above criteira or else C# code is also helpful for me.

Let me suggest a different approach: Instead of creating a fancy regular expression that confuses everyone reading it (including you, since you cannot come up with one on your own), just encode the logic in a few simple C# statements:
if (mypassword.Length < 8)
myerror = "The password must have a minimum length of 8 characters.";
else if (!Regex.IsMatch(mypassword, "[A-Z]"))
myerror = "The password must contain at least one capital letter.";
...

Related

Simple regex for email validation using C#. Has # symbol in the middle [duplicate]

This question already has answers here:
What's wrong with this RegEx for validating emails?
(5 answers)
Closed 2 years ago.
I don't know anything about regex.
How do I create an email validation that allows such entries?
Allow
a1b2c3#a1b2c3
a#a
1#1
a1#a#a2e
!s#s$ds
Dont Allow
#
###
!##
Basically allow characters that has # in the middle
If you aren't fluent in regex you can split the string at "#" and expect two elements.
If you want to use regex, the first result on google was: https://emailregex.com

Generate Email Addresses with Special Characters [duplicate]

This question already has answers here:
How do I remove diacritics (accents) from a string in .NET?
(22 answers)
Closed 3 years ago.
I am tasked with generating an email address for new users in our environment who may have special characters (such as an umlaut and accents) in their full names. While some email systems can handle these characters I am being told to generate email addresses that can convert these characters to a standard ASCII format.
So for example, John Señior would be created as john.senior#... and Jane Fraü would be created as jane.frau#...
Has anyone found a regex expression or library that can handle such a requirement.
UPDATE - I am planning on performing the replacement in PowerShell, but could use any Windows compiled libraries as well.
Having a better understanding of what I was trying to do, aka removing diacritics, I was directed to this article How do I remove diacritics (accents) from a string in .NET? which provided a good solution that I converted into use for PowerShell:
$name = "Jane Fraü"
$tempBytes = [System.Text.Encoding]::GetEncoding("ISO-8859-8").GetBytes($name)
$newName = [System.Text.Encoding]::UTF8.GetString($tempBytes)
Write-Host $newName
Jane Frau

Regex for atleast 1 special character, 1 number and 1 letter, min length 8 characters C# [duplicate]

This question already has answers here:
Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters
(42 answers)
Closed 4 years ago.
I am trying to validate the password. I am using Data Annotations in my ViewModels like below:
[RegularExpression("^(?=.*[A-Za-z])(?=.*[0-9])(?=.*[#$!%*#?&])[A-Za-z[0-9]#$!%*#?&]{8,}$",ErrorMessage="Password must contain atleast 1 number, 1 letter, and 1 special character.")]
When I try to register with the right format, it still gives me the error message.
Can someone please look at it and help me out?
You can use this regular expression to validate content
([a-zA-Z]{1,})([#$!%*#?&]{1,})([0-9]{1,})
But I would use another data-annotation attribute to validate length. Then you will know if your ModelState is failing because of invalid characters or length, for example [StringLength]
In your regex, you have
[A-Za-z[0-9]#$!%*#?&]
You don't need the inner square brackets for [0-9]. It should be 0-9 or \d.

Regular expression for my password policy [duplicate]

This question already has answers here:
Regular expression to enforce complex passwords, matching 3 out of 4 rules
(4 answers)
Closed 8 years ago.
My password strength criteria is as below :
Character length: 8 -> 24
Contains least 3 of following types:
Uppercase letter [A-Z]
Lowercase letter [a-z]
Numeric [0-9]
Special character ~`!##$%^&*()-_=+[{]}\|;:'"<,>.?/
Can anyone help me to make regular expression and explain. Thanks.
It would be very difficult to do in one regex. Personally, I'd check for each case separately and count if you've got three matches. It would be easier to read and maintain.
So match [A-Z] then [a-z] then [0-9] and finally [~`!##$%^&*()-_=+[{]}\|;:'"<,>.?/]
If you end up with a match in three of the tests that's success.
Use this Regex:
(?=.{8,24})(?=.*\d)(?=.*[A-Za-z])(?=.*[~`!##\$%\^&\*()_=\+[{\]}\|;:'"<,>\.\?/]).*
Debuggex Demo

Regex for matching US phone number with or without area code [duplicate]

This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 9 years ago.
My question was marked as a duplicate so I've made a couple edits. As I said, I was able to find many similar questions when I searched but none were quite what I needed. I am not validating a string where the only thing present will be the phone number (this seems to be what most of the other questions are addressing). Rather, I am attempting to pull out all phone numbers (which will then be manually checked by the user) from a larger block of text. The problem I am having is that my regular expression is matching zip codes with extensions (ex: 45202-4787), and I am not sure how to alter my regex to avoid that. If this truly is a duplicate question then I apologize for not being able to find the existing one that deals with my issue.
My specifications for phone number format are:
1) -, ., and space as delimiters (and in any combination)
2) area code may appear with or without parentheses
A few examples:
(xxx) xxx-xxxx
(xxx) xxx.xxxx
xxx-xxx-xxxx
xxx xxx-xxxx
xxxxxxxxxxx
I am using Anirudh's regex from the comments:
(\(?\d{3}\)?)?[. -]?\d{3}[. -]?\d{4}
Again, my problem is that this regex matches zip codes with extensions (ex: 45202-4787).
I would be grateful for any help, as I'm very new to using regular expressions. Thanks!
This should do it:
^(\([0-9]{3}\)|[0-9]{3})[ -\.]?[0-9]{3}[ -\.]?[0-9]{4}$

Categories