-4

What is the regex to check if input string is NOT lowercase only, it is NOT uppercase only and does NOT contain numbers.

Validation must fail

SIMO TEST
SIMO344
simo
simo3432

These are ok

SIMO test
Simo
Simo Mafuxwana
  • 3,702
  • 6
  • 41
  • 59

3 Answers3

1

Welcome to Stackoverflow

When posting a question, please make sure to include your attempts, so that we can help guide you to an answer. This website is not meant to give you the answer, but to help guide you to an answer, or to explain your error. It's more rewarding to help you if we are simply guiding you and not giving you the answer. You'll probably get more response too. Please remember that when posting next time.

I tried to explain regular expressions in JavaScript, and tried to guide you through the logic in my answer.

Your case

You can use the .test function of a RegExp to test if a string matches a regular expression. You can then invert that result to check if the string does not contain it. Each of the cases you mentioned is a separate expression, which can be joined by the | operator.

Testing if a string is lowercase only:

In a RegExp, a - can be used to indicate a range of characters. There are already specially assigned codes for commonly used ranges, such as \s for a white space. The + operator means one or more. The ^ means starts at the beginning of the line(string) and $ means starting the end.

^[a-z\s]+$

Testing if a string is uppercase only:

This is the exact same as the lowercase case, but the character range is for uppercase letters:

^[A-Z\s]+$

Testing for digits

The regex code \d is short for a range of digits (you can essentially think of it as [0-9], but it also accounts for unicode).

\d

Putting it all together

^[a-z\s]+$|^[A-Z\s]+$|\d

And in a condition, it would be:

if (!/^[a-z\s]+$|^[A-Z\s]+$|\d/.test(your_string_here)) {
    // the string isn't uppercase only, lowercase only
    // and doesn't contain a digit
} 
hargasinski
  • 841
  • 5
  • 14
  • thank you this is what I was looking for. The thing is there are several questions on stack that have no cod eexamples, no error snippets yet you can still workout what the person was trying to do but somehow mine is a BIG problem. Like these http://stackoverflow.com/questions/6540393/regex-to-validate-a-string-whether-it-contains-at-least-one-lower-case-char-up and http://stackoverflow.com/questions/13353663/what-is-the-regular-expression-to-allow-uppercase-or-lowercase-alphabetical-char#_=_ have no code examples but you can make out what the person is trying to do. – Simo Mafuxwana Apr 08 '16 at 10:29
1

Please see the below code snippet. Modify as per your requirement.

function validate(strInput) {
    var re = /\d/;  
    if(re.exec(strInput)===null){
        re = /^(?!.*[a-z\d]).+$/;
        if(re.exec(strInput)===null){
            re = /^[A-Z][a-z]*/;  
            if(re.exec(strInput)!==null)
                return re.exec(strInput);
        }
    }

    return false;
};

console.log(validate("SIMO TEST"));
console.log(validate("SIMO344"));
console.log(validate("Simo"));
console.log(validate("simo"));
console.log(validate("simo3432"));
console.log(validate("SIMO2 TEST"));
console.log(validate("Simo3"));
console.log(validate("SIMO test"));
0
      function CheckPassword() {
            var inputtxt = $('#text12').val();
            console.log(inputtxt)
            var passw = /(?=.*[a-z])(?=.*[A-Z]).{6,20}$/;
            var passWN = /\d/;
            if (inputtxt.match(passw)) {
                if (!inputtxt.match(passWN)) {
                    alert('Correct, try another...')
                    return true;
                } else {
                    alert('Wrong...!')
                    return false;
                }
            } else {
                alert('Wrong...!')
                return false;
            }
        }
Rajiv
  • 78
  • 10