2

This is what I have so far,

^(?=.'{'8,14'}'$)(?=.*[0-9])(?=.*[!@#$%^&*])(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9!@#$%^&*]).*$ 

which allows one atleast one lowercase , one uppercase, one number and on special character with minimun length 8 and max length 14.

And if i don't want any number then I am using this

^(?=.'{'8,14'}')(?=.*^([^0-9]*)$)(?=.*[!@#$%^&*])(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9!@#$%^&*]).*$

But if I want that it should not accept any lowercase then what it should look like? And if later on I want to accept lowercase and not uppercase then how it should be or if I want to remove special character then how the expression will look like. I don't have any idea on javascript and regex thing so please help me with the expression and I am not allowed to do it using iteration.

Note: '{'8,14'}' here single quote is just used as escape sequence so please don't bother about that

2
  • The easiest solution is to break up your regex to handle each tests individually like one for lower case another for upper and invoke them only if it is needed Commented May 11, 2015 at 5:56
  • (?=.[a-z]) if I remove this then it should not accept the lowercase right? But still it is accepting it Commented May 11, 2015 at 5:57

2 Answers 2

1

It may be clearer/easier to split up your logic into multiple regexes and run the string through each of them:

var rules = [
  /^.{8,14}$/,
  /[a-z]/,
  /[A-Z]/,
  /[0-9]/,
  /[!@#$%^&]/
];

function isValid(str) {
  for (var i = 0, l = rules.length; i < l; i++)
    if (!rules[i].test(str)) return false;
  return true;
}

isValid('abc'); // false
isValid('a0%Aeeee'); // true

You can also add a final /^[a-zA-Z0-9!@#$%^&]+$/ to ensure that only the characters you specify have been used at all.

Sign up to request clarification or add additional context in comments.

1 Comment

/^[a-zA-Z0-9!@#$%^&]+$/ should be incorporated into /^.{8,14}$/, i.e. /^[a-zA-Z0-9!@#$%^&]{8,14}$/
1

But if I want that it should not accept any lowercase then what it should look like?

Then you can add negative lookahead .

(?!.*[a-z])

And if later on I want to accept lowercase and not uppercase then how it should be

(?!.*[A-Z])

minimun length 8 and max length 14.

For this you dont need lookahead.Just change .*$ to .{8,14}$.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.