.NET Framework Developer Center > .NET Development Forums > Regular Expressions > How to check for space in a password regex having multiple condition
Ask a questionAsk a question
 

AnswerHow to check for space in a password regex having multiple condition

  • Friday, November 06, 2009 3:25 PMRajendra Dewani Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi
    I have a requirment where in the password field should accept all keys except space, it should have atleast one number , length should be 6 to 50
    I tried following but did not work
    1) Regex = @"^.*(?=.{6,50})(?=.*\d)(?=.*[a-zA-Z]).*$"

    2) ^(?=[^\s])(?=[^\s]{6,50})(?=[^\s]*\d)(?=[^\s]*[a-zA-Z]).*$

    Following are my test input text ( test case no 2 is having space at end)

     ShouldNOT654321-1
    ShouldNOT654321-2
    Should654321-3
    ShouldNOT 654321-4
    Should NOT654321-5
    S houldNOT654321-6
    ShouldNOT65432 1-7
    S houldNOT 654321-9
    Should654321
    1Should654321

    Please help.

    Thanks in Advance.
    Rajendra Dewani


    Regards Rajendra Dewani

Answers

  • Wednesday, November 11, 2009 11:53 AMGregory Adam Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Would this work ?

    Part one of the pattern asserts that there's at least one digit ahead
    Part two asserts at least one a-zA-Z
    Part three matches 6 to 50 non space chars.  From parts one and two we know that the non space chars contain at least one digit and at least one  a-zA-Z

    	static void Main()
    		{
    
    			// lookahead for one digit
    			// lookahead for one a-zA-Z
    			// match 6 to 50 non space chars
    			string pattern = @"^(?=.*\d.*)(?=.*[a-zA-Z].*)([^\s]{6,50})$";
    
    
    			string[] test = {
    								@" ShouldNOT654321-1",
    								@"ShouldNOT654321-2 ",
    								@"Should654321-3",
    								@"ShouldNOT 654321-4",
    								@"Should NOT654321-5",
    								@"S houldNOT654321-6",
    								@"ShouldNOT65432 1-7",
    								@"S houldNOT 654321-9",
    								@"Should654321",
    								@"1Should654321",
    								@"nono",
    								@"nonono",
    								@"11111111",
    								@"1a......",
    								@"........."
    							};
    
    			foreach ( string s in test )
    			{
    				Match match = Regex.Match(s, pattern);
    				Console.WriteLine("{0}\t{1}", match.Success, s);
    		
    
    			}
    
    			Console.ReadLine();
    		}
    
    output:
    False    ShouldNOT654321-1
    False   ShouldNOT654321-2
    True    Should654321-3
    False   ShouldNOT 654321-4
    False   Should NOT654321-5
    False   S houldNOT654321-6
    False   ShouldNOT65432 1-7
    False   S houldNOT 654321-9
    True    Should654321
    True    1Should654321
    False   nono
    False   nonono
    False   11111111
    True    1a......
    False   .........
  • Wednesday, November 11, 2009 1:53 PMRajendra Dewani Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi , I tried this and its working Regex = @"^[^ ]*([a-zA-Z\S*])+((\S*[a-zA-Z]+\S*\d+\S*)|(\S*\d+\S*[a-zA-Z]+\S*))$"
    Regards Rajendra Dewani

All Replies

  • Saturday, November 07, 2009 5:31 AMDheeraj_Mallemala Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer
    Hi,

    Try below code, it accepts all special characters, except white space.
    tested will all above conditions, and it works.

          
                string input_to_test = @"ShouldNOT 654321-4";

                Match match_object = Regex.Match(input_to_test, @"^(?([^ ]{6,50})(?(\d+)(\d+\S+)|(\S+\d+\S*)))$");
                if (match_object.Success)
                    Console.WriteLine("Success");
                else
                    Console.WriteLine("No Luck. Try again...!");
            
     
  • Monday, November 09, 2009 6:32 AMRajendra Dewani Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Dheeraj,

    Thank for the regex.
    The regex is working but i forgot one condition.
    the password should have atleast one char and one numeric value

    Please help to add this condition .

    Thanks in advance.

    Regards
    Rajendra
    Regards Rajendra Dewani
  • Monday, November 09, 2009 6:49 AMRajendra Dewani Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I found one issue with the regex is , it is allowing empty value

    Regards Rajendra Dewani
  • Wednesday, November 11, 2009 11:53 AMGregory Adam Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Would this work ?

    Part one of the pattern asserts that there's at least one digit ahead
    Part two asserts at least one a-zA-Z
    Part three matches 6 to 50 non space chars.  From parts one and two we know that the non space chars contain at least one digit and at least one  a-zA-Z

    	static void Main()
    		{
    
    			// lookahead for one digit
    			// lookahead for one a-zA-Z
    			// match 6 to 50 non space chars
    			string pattern = @"^(?=.*\d.*)(?=.*[a-zA-Z].*)([^\s]{6,50})$";
    
    
    			string[] test = {
    								@" ShouldNOT654321-1",
    								@"ShouldNOT654321-2 ",
    								@"Should654321-3",
    								@"ShouldNOT 654321-4",
    								@"Should NOT654321-5",
    								@"S houldNOT654321-6",
    								@"ShouldNOT65432 1-7",
    								@"S houldNOT 654321-9",
    								@"Should654321",
    								@"1Should654321",
    								@"nono",
    								@"nonono",
    								@"11111111",
    								@"1a......",
    								@"........."
    							};
    
    			foreach ( string s in test )
    			{
    				Match match = Regex.Match(s, pattern);
    				Console.WriteLine("{0}\t{1}", match.Success, s);
    		
    
    			}
    
    			Console.ReadLine();
    		}
    
    output:
    False    ShouldNOT654321-1
    False   ShouldNOT654321-2
    True    Should654321-3
    False   ShouldNOT 654321-4
    False   Should NOT654321-5
    False   S houldNOT654321-6
    False   ShouldNOT65432 1-7
    False   S houldNOT 654321-9
    True    Should654321
    True    1Should654321
    False   nono
    False   nonono
    False   11111111
    True    1a......
    False   .........
  • Wednesday, November 11, 2009 1:53 PMRajendra Dewani Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi , I tried this and its working Regex = @"^[^ ]*([a-zA-Z\S*])+((\S*[a-zA-Z]+\S*\d+\S*)|(\S*\d+\S*[a-zA-Z]+\S*))$"
    Regards Rajendra Dewani
  • Wednesday, November 11, 2009 3:05 PMGregory Adam Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    (1) Try with 1a....
    (2) What happened to the 50 char limit ?

  • Thursday, November 12, 2009 5:50 AMRajendra Dewani Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Adam,

    Regex worked.

    it works perfectly on .Net. 
    I tried the same on Jquery, in Jquery the behaviour is different. it request of atleat 6 numeric.
    any Clue....


    Regards Rajendra Dewani