SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot Mr. Ram's Avatar
    Join Date
    Nov 2005
    Posts
    139
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)

    Question Password (String) Validations

    I have to validate the user-password based on below conditions.

    • Password should contain atleast one digit.
    • Password should contain atleast one upper character.
    • Password should contain atleast one lower character.
    • Password should contain atleast one special character.
    • Password should not contain successive repeated characters.

    I have done as below. Please let me know if there is any simple approach.
    Code:
    function isItContainsDigit(fieldname)
    {
    
    	var numaric = fieldname.value;
    	var temp =false;
        
    	for(var j=0; j<numaric.length; j++)
    	{
    
    		var alphaa = numaric.charAt(j);
    		var hh = alphaa.charCodeAt(0);
    
    		if(hh > 47 && hh<58) //Digit
    		{
    
    			temp =true;
    			break;
    		}
    	}
    	return temp;
    }
    
    function isItContainsUpperChar(fieldname)
    {
    
    	var numaric = fieldname.value;
    	var temp =false;
    
        for(var j=0; j<numaric.length; j++)
        {
    
    		var alphaa = numaric.charAt(j);
    		var hh = alphaa.charCodeAt(0);
    
    		if(hh > 64 && hh<91) //Upper
    		{
    			temp =true;
    			break;
    		}
    	}
    	return temp;
    }
    
    function isItContainsLowerChar(fieldname)
    {
    	var numaric = fieldname.value;
    	var temp =false;
    
    	for(var j=0; j<numaric.length; j++)
    	{
    
    		var alphaa = numaric.charAt(j);
    		var hh = alphaa.charCodeAt(0);
    
    		if(hh > 96 && hh<123) //Lower Case
    		{
    			temp =true;
    			break;
    		}
    	}
    	return temp;
    }
    
    function isItContainsSpecial(fieldname)
    {
    	var numaric = fieldname.value;
    	var temp =false;
        
    	for(var j=0; j<numaric.length; j++)
    	{
    	
    		var alphaa = numaric.charAt(j);
    		var hh = alphaa.charCodeAt(0);
    
    		if(hh > 32 && hh<48) //Special char
    		{
    			temp =true;
    			break;
    		}
    	}
    	return temp;
    }
    
    function isItContainsRepeatedChars(fieldname)
    {
    
    	var numaric = fieldname.value;
    	var temp =false;
    	var oldChar = '';
    
    	for(var j=0; j<numaric.length; j++)
    	{
    
    		var alphaa = numaric.charAt(j);
    
    		if(oldChar == alphaa)	// check with old char
    		{
    			temp = true;
    			break;
    		}
    		else
    		{
    
    			oldChar = alphaa;
    		}
    	}
    	return temp;
    }

  2. #2
    SitePoint Guru Ruben K.'s Avatar
    Join Date
    Jun 2005
    Location
    Alkmaar, The Netherlands
    Posts
    693
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using a regular expression for this would be one line instead of 60+

  3. #3
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The first five criteria would be simple to do with regular expressions:
    Code JavaScript:
    function validate(pwd) {
    if (!/\d/.test(pwd))
        alert("Password must contain at least one digit.");
    else if (!/[A-Z]/.test(pwd))
        alert("Password must contain at least one uppercase letter.");
    else if (!/[a-z]/.test(pwd))
        alert("Password must contain at least one lowercase letter.");
    else if (!/[^A-Za-z0-9]/.test(pwd))
        alert("Password must contain at least one special character.");
    }
    My brain isn't working right now, so I can't come up with a regex for the last case.
    Birnam wood is come to Dunsinane


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •