.NET Framework Developer Center >
.NET Development Forums
>
Regular Expressions
>
How to check for space in a password regex having multiple condition
How to check for space in a password regex having multiple condition
- 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
- 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
output: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(); }
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 .........
- Marked As Answer byJialiang Ge [MSFT]MSFT, ModeratorWednesday, November 18, 2009 6:17 AM
- 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- Marked As Answer byJialiang Ge [MSFT]MSFT, ModeratorWednesday, November 18, 2009 6:18 AM
All Replies
- 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");elseConsole.WriteLine("No Luck. Try again...!");
- Proposed As Answer byDheeraj_Mallemala Saturday, November 07, 2009 5:56 AM
- 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 - I found one issue with the regex is , it is allowing empty value
Regards Rajendra Dewani - 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
output: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(); }
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 .........
- Marked As Answer byJialiang Ge [MSFT]MSFT, ModeratorWednesday, November 18, 2009 6:17 AM
- 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- Marked As Answer byJialiang Ge [MSFT]MSFT, ModeratorWednesday, November 18, 2009 6:18 AM
(1) Try with 1a....
(2) What happened to the 50 char limit ?- 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


