Click here to Skip to main content
14,305,681 members
Rate this:
Please Sign up or sign in to vote.
See more:
Dear All

I need regular expression for validating same in textbox

minLength: 8,
maxLength: 16,
upperCase: 0,
numbers: 1,
lower case:2 ,
specialChars:Ntt allowed
Posted
Updated 26-Mar-12 23:54pm
v2
Comments
Amir Mahfoozi 27-Mar-12 6:46am
   
Are you sure that it can be implemented with regular expressions ?
ProEnggSoft 27-Mar-12 6:51am
   
It can be implemented. Please see my solution (2)
ProEnggSoft 27-Mar-12 7:22am
   
+5 for a thought provoking question.
ProEnggSoft 28-Mar-12 6:03am
   
If your question is answered, you may consider to accept the solution.
Rate this:
Please Sign up or sign in to vote.

Solution 2

The following regular expression meets the above requirement

(?(?=^[a-z0-9]{8,16}$)(?(?=^.*\d.*\d.*$)(?(?=^.*[a-z].*[a-z].*$)^[a-z0-9]{8,16}$)))

It first searches for alpha numeric string of 8 to 16 characters length, then
it searches if there are atleast two numbers and then searches for atleast two small alphabet. Only then the search passes.

It can be tested online here
http://regexhero.net/tester/[^]
   
v4
Comments
Dalek Dave 27-Mar-12 7:00am
   
Yes I agree.

I just couldn't see what the OP wanted at first, his question was not as clear as it could have been.
ProEnggSoft 27-Mar-12 7:06am
   
Thank you.
Amir Mahfoozi 27-Mar-12 7:18am
   
+5 I tested it here : http://regexhero.net/tester/ and it seems that it works well.
ProEnggSoft 27-Mar-12 7:20am
   
Thank you.
Amir Mahfoozi 27-Mar-12 7:23am
   
It was not my question :)
ProEnggSoft 27-Mar-12 7:24am
   
Extremely sorry. I noticed later and modified my comment.
Amir Mahfoozi 27-Mar-12 7:25am
   
No problem ;)
Espen Harlinn 27-Mar-12 9:18am
   
5'ed!
ProEnggSoft 27-Mar-12 12:08pm
   
Thanks a lot.
Rate this:
Please Sign up or sign in to vote.

Solution 1

^[0-9a-z](8,16)$

Should do it.

0 to 9 and a to z lower case, minimum length 8 max 16, no special characters added.
   
Comments
mayur csharp G 27-Mar-12 6:14am
   
Needed
Numbers exactly 1
lowercase exactly 2
Dalek Dave 27-Mar-12 6:27am
   
Please explain how that works?
You need Exactly 1 number, ok easy.
You need Exactly 2 lowercase, again, simple.
You state No Uppercase at all, no problem
You require Zero special character, child's play.

You want it between 8 and 16 characters - er... what the hell are you putting in there?
You cannot have zero upper case, 1 number and 2 lower case with no special characters.

Perhaps you are not asking your question clearly enough.
mayur csharp G 27-Mar-12 6:33am
   
sorry Dalke i got it

I need exactly

2 small case characters more than 2 allowed

2 digits more than 2 allowed
Dalek Dave 27-Mar-12 6:38am
   
So MINIMUM of 2 lower case, MINIMUM of 2 Numbers?
mayur csharp G 27-Mar-12 6:40am
   
yes dear
ProEnggSoft 27-Mar-12 6:52am
   
Please see my solution (2), whether it works for you.
mayur csharp G 27-Mar-12 7:01am
   
no it is not working
ProEnggSoft 27-Mar-12 7:06am
   
I have checked it. It is working. Please give your test string. So that I will also test. Thank you.
mayur csharp G 27-Mar-12 7:07am
   
function passwordChanged() {
var strength = document.getElementById('strength');
//var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
//var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
//var enoughRegex = new RegExp("(?=.{6,}).*", "g");
var last = new RegExp("(?(?=^[a-z0-9]{8,16}$)(?(?=^.*\d{1}.*\d{1}.*$)(?(?=^.*[a-z]{1}.*[a-z]{1}.*$)^[a-z0-9]{8,16}$)))");
var pwd = document.getElementById("pwdPassword");
if (pwd.value.length==0) {
strength.innerHTML = 'Type Password';
//} else if (false == enoughRegex.test(pwd.value)) {
//strength.innerHTML = 'More Characters';
//} else if (strongRegex.test(pwd.value)) {
//strength.innerHTML = '<span style="color:green">Strong!</span>';
//} else if (mediumRegex.test(pwd.value)) {
//strength.innerHTML = '<span style="color:orange">Medium!</span>';
//}
}
else if (last.test(pwd.value)) {
strength.innerHTML = '<span style="color:orange">Medium!</span>';
}
else {
strength.innerHTML = '<span style="color:red">Weak!</span>';
}

}
ProEnggSoft 27-Mar-12 7:12am
   
Can you provide some test string to test for.
ProEnggSoft 27-Mar-12 7:16am
   
You have to put @ in front of pattern, as @"(?......

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100