Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hello guys,


please suggest me validation code for following conditions

1.Password must have a minimum of eight (8) characters.
2. Password must have at least three (3) of the four (4) character types: lowercase alphabetic, uppercase alphabetic,numeric, and special characters.
3.Password must not be the same as the user identifier.
4. Password must not contain three (3) or more repeating characters.


thanks
Rajshree lande
Posted
Comments
[no name] 6-Oct-14 3:37am    
The way to get help is to ask about a specific problem and perhaps provide the code you are working on.

Implementation of such validation code is pretty trivial (so consider doing it yourself and ask here only specific questions). For instance, suppose the (string) variable entered by the user is pwd, the you may check point (1) using the following code:
C#
if (pwd.Lenght < 8)
{
  return false;
}


Other points are just a little bit more complex.
 
Share this answer
 
^(?=.*[A-Z])(?=.*\d)(?!.*(.)\1\1)[a-zA-Z0-9@]{6,12}$

Special Characters - Not Allowed
Spaces - Not Allowed
Minimum and Maximum Length of field - 6 to 12 Characters
Met by [a-zA-Z0-9@]{6,12}
Numeric Character - At least one character
Met by positive lookahead (?=.*\d)
At least one Capital Letter
Met by positive lookahead (?=.*[A-Z])
Repetitive Characters - Allowed only two repetitive characters
I am not sure what you mean by this. The negative lookahead (?!.*(.)\1\1) makes sure that no character is allowed to appear more than two times in a row. Substring aa is okay, aaa is not.
Make it (?!.*(.+)\1\1) to reject repeated substrings of length more than one (like ababab) or add .* before \1 to reject non-continuous repeated appearances too.

refer: http://stackoverflow.com/questions/12899876/checking-strings-for-a-strong-enough-password[^]


OR

You can do these checks using positive look ahead assertions:

<pre lang="xml">^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&amp;*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{8}$
<a href="http://www.rubular.com/r/UAwoaPM0Ji">Rubular link</a>[<a href="http://www.rubular.com/r/UAwoaPM0Ji" target="_blank" title="New Window">^</a>]

Explanation:

^                         Start anchor
(?=.*[A-Z].*[A-Z])        Ensure string has two uppercase letters.
(?=.*[!@#$&amp;*])            Ensure string has one special case letter.
(?=.*[0-9].*[0-9])        Ensure string has two digits.
(?=.*[a-z].*[a-z].*[a-z]) Ensure string has three lowercase letters.
.{8}                      Ensure string is of length 8.
$                         End anchor.</pre>


refer: regex-for-password-strength[^]


OR

public static bool IsStrongPassword(string password)
{
    // Minimum and Maximum Length of field - 6 to 12 Characters
    if (password.Length &lt; 6 || password.Length &gt; 12)
        return false;

    // Special Characters - Not Allowed
    // Spaces - Not Allowed
    if (!(password.All(c =&gt; char.IsLetter(c) || char.IsDigit(c))))  
        return false;

    // Numeric Character - At least one character
    if (!password.Any(c =&gt; char.IsDigit(c)))
        return false;

    // At least one Capital Letter
    if (!password.Any(c =&gt; char.IsUpper(c)))
        return false;

    // Repetitive Characters - Allowed only two repetitive characters
    var repeatCount = 0;
    var lastChar = '\0';
    foreach(var c in password)
    {
        if (c == lastChar)
            repeatCount++;
        else
            repeatCount = 0;
        if (repeatCount == 2)
            return false;
        lastChar = c;
    }

    return true;
}

refer: strong-password-regex[^]
 
Share this answer
 
Comments
rajshreelande 6-Oct-14 8:21am    
@Leena 206

i have question on dis
Password must not contain three (3) or more repeating characters.
can u please sussgest me code ..

ex..1.abcbub-must not allowed,abbb-must not allowed,abb-allowed,ahbua-allowed
RETURNS IF IT IS VALID PASSWORD OR NOT


ALTER FUNCTION [dbo].[IsValidPassword]
(
@LoginID NVARCHAR(50),
@inputPassword NVARCHAR(50)
)
RETURNS int
AS
BEGIN

DECLARE @LoginCompId int;
DECLARE @MinLength int=6;
DECLARE @Result BIT = 0
DECLARE @Counter INT = 1
DECLARE @IsValidPassword INT = 0
DECLARE @Count int=0
DECLARE @outputvalue varchar(200)
DECLARE @isValid Char(1);

SELECT @LoginCompId = comp_id FROM mst_user_info with(nolock) WHERE login_id = @LoginID
SELECT @MinLength = para_val FROM sys_param with(nolock) WHERE comp_id = @LoginCompId and para_name='PASSWORD_MINLENGTH'

select @isValid=dbo.CheckValidString(@inputPassword)
--WHILE (@Counter <= LEN(@inputPassword) - 1)
--BEGIN
--IF(ASCII((SELECT SUBSTRING(@inputPassword, @Counter, 1))) = ASCII((SELECT SUBSTRING(@inputPassword, @Counter + 2, 1))))
-- BEGIN
-- SET @Result = 1
-- BREAK
-- END
-- SET @Counter = @Counter + 1
--END

IF(@isValid='Y')
BEGIN
IF( @inputPassword COLLATE Latin1_General_BIN LIKE '%[a-z]%' )
BEGIN
SET @Count=@Count+1
END

IF(@inputPassword COLLATE Latin1_General_BIN LIKE '%[A-Z]%' )
BEGIN
SET @Count=@Count+1
END

IF(@inputPassword LIKE '%[0-9]%')
BEGIN
SET @Count=@Count+1
END

IF(@inputPassword LIKE '%[~!@#$%^&]%')
BEGIN
SET @Count=@Count+1
END

SELECT @IsValidPassword=CASE WHEN
LEN(@inputPassword) >= @MinLength AND
@Count >= 3
THEN 1 ELSE 0 END
END
ELSE
BEGIN
SET @IsValidPassword=0
END


RETURN @IsValidPassword
END

----------------

CheckValidString

ALTER FUNCTION [dbo].[CheckValidString]
(
@input varchar(50)

)
RETURNS varchar
AS
BEGIN

declare @inputLength integer = datalength(@input);
declare @intIndex integer = 1;
declare @intMaxCount integer = 3;
declare @singleChar Char(1);
declare @isValid Char(1);
while (1 = 1)
begin
if @intIndex > @inputLength
begin
select @isValid = 'Y';
break;
end

select @singleChar = substring(@input, @intIndex, 1)

if(@inputLength - datalength(replace(@input, @singleChar, '')) > @intMaxCount)
begin
select @isValid = 'N';
break;
end
select @intIndex += 1;
end

RETURN @isValid
END
-------------
sp-----[UspValidatePassword]
-----------------------------------


ALTER PROCEDURE [dbo].[UspValidatePassword]
@LoginID NVARCHAR(50),
@inputPassword NVARCHAR(50),
@ErrorMsg varchar(250) OUTPUT

AS
BEGIN

DECLARE @IsValidPassword int
DECLARE @CheckPassWithUserid int
DECLARE @MinLength varchar=6;
DECLARE @LoginCompId int;
SET @ERRORMSG='';

SELECT @LoginCompId = comp_id FROM mst_user_info with(nolock) WHERE login_id = @LoginID
SELECT @MinLength = para_val FROM sys_param with(nolock) WHERE comp_id = @LoginCompId and para_name='PASSWORD_MINLENGTH'

if(@LoginID<>@inputPassword)
BEGIN
SELECT @ISVALIDPASSWORD=[dbo].[IsValidPassword](@LOGINID,@INPUTPASSWORD)
print(@ISVALIDPASSWORD)
IF(@ISVALIDPASSWORD=0)
BEGIN
SET @ERRORMSG='Incorrect Password ! Password must be of minimum '+@MinLength+' characters and contain any 3 combination of Digit, Upper Case, Lower Case and Special Character.';
print @ERRORMSG
END

END
ELSE
BEGIN
SET @ERRORMSG='Login ID and Password should not be same !';
END
END
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900