locked
How to have Password Regex? RRS feed

  • Question

  • User969992919 posted

    Hi,

    I would like to ask how can I have a Regex for password to prevent space and to accept only numbers OR letters OR alphanumeric, minimum 6 and max of 20 characters?

    Thanks,

    Jassim

    Monday, July 16, 2018 8:24 AM

All replies

  • User-492460945 posted

    Hi,

    This link may help you.

    Thanks,

    RajeshV.

    Monday, July 16, 2018 8:48 AM
  • User-369506445 posted

    hi

    please try below regex

    "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)\S{6,20}$"

    and example for web Form

    <form runat="server">
    
            <asp:TextBox ID="textbox1" runat="server"></asp:TextBox><br />
            <asp:RegularExpressionValidator ID="Regex1" runat="server" ControlToValidate="textbox1"
                ValidationExpression="^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)\S{6,20}$"
                ErrorMessage="Minimum 6 Max 20 characters atleast 1 Alphabet, 1 Number and 1 Special Character and avoid space" ForeColor="Red" />
            <asp:Button runat="server" Text="validate" />
    
        </form>

    for MVC

            [Required]
            [RegularExpression(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)\S{6,20}$", ErrorMessage = "Invalid Password")]
            public int password { get; set; }

    Monday, July 16, 2018 8:55 AM
  • User-1171043462 posted

    Regex

    ^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,20}$

    ASP.Net Validator

    <asp:TextBox ID="txtPolicy1" runat="server"></asp:TextBox><br />
    <asp:RegularExpressionValidator ID="Regex1" runat="server" ControlToValidate="txtPolicy1"
        ValidationExpression="^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,20}$" ErrorMessage="Password must contain: Minimum 8 characters atleast 1 Alphabet and 1 Number" ForeColor="Red" />
    

    Ref: Implement Password Policy using Regular Expressions and ASP.Net RegularExpression Validator

    ASP.Net MVC

    public class PersonModel
    {
        [Display(Name = "Password")]
        [Required(ErrorMessage = "Password is required.")]
        [RegularExpression("^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,20}$", ErrorMessage = "Only Alphabets and Numbers allowed.")]
        public string UserName { get; set; }
    }
    

    Ref: RegularExpression Data Annotation example

    Monday, July 16, 2018 12:09 PM
  • User-330142929 posted

    Hi Jrahma,

    According to your description, I think we could use the following Regular Expression.

    string pattern = @"^[a-zA-Z0-9]{6,20}$";

    you could check whether the Regex match what you want by using the following code.

    string pattern = @"^[a-zA-Z0-9]{6,20}$";
                string pattern1 = @"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,20}$";
                //string pattern2 = @"^(?=.*[A-Za-z0-9])[A-Za-z\d]{6,20}$";
                Regex regex = new Regex(pattern); 
                string text = this.TextBox1.Text;
                var result = regex.IsMatch(text);
    ClientScript.RegisterStartupScript(this.GetType(), "", "alert('"+result+"')", true);

    Feel free to let me know if you have any questions

    Best Regards

    Abraham

    Tuesday, July 17, 2018 1:08 PM
  • User1204533129 posted

    try this 

    Best regards ,

    kamal

    /[^A-Za-z0-9]{6,30}+/g
    Thursday, October 4, 2018 8:10 PM