2

I need to make a password validation where you can only register if your password contains an Uppercase and lowercase letters. But i can't find out how to detect if a letter is uppercase or lowercase. I Hope someone is able to help me with it :)

Here's my code till now! :

  if(!username.Contains("@nhl.nl")){
    errorMessage = "Voer een geldig NHL adres in.";
} 
else 
{
    if(DOESN'T CONTAIN UPPER CASE GIVE ERROR! ){
        errorMessage = "Het wachtwoord moet een hoofdletter bevatten!";
    }
    else {
    if(password.Length < 8){
        errorMessage = "Je wachtwoord moet minimaal 8 karakters bevatten!";
    }
    else {
        if(password != confirmPassword){
          errorMessage = "Wachtwoorden komen niet overeen!";
        }
        else {
          if(WebSecurity.UserExists(username)){
            errorMessage = String.Format("User '{0}' already exists.", username);
          }
          else{
            WebSecurity.CreateUserAndAccount(username,password,null,false);
            WebSecurity.Login(username, password, true);
            errorMessage = String.Format("{0} created.", username);
            }
          }
          }
      }
    }
3
  • 2
    Isupper and Islower functions are in c# for this. Commented Mar 4, 2015 at 12:34
  • @SainPradeep Yes it is, but how can i implement it into the if statement? Commented Mar 4, 2015 at 12:35
  • Please refer this stackoverflow.com/questions/20032450/… Commented Mar 4, 2015 at 12:37

4 Answers 4

3

Replace:

if(DOESN'T CONTAIN UPPER CASE GIVE ERROR! ){
    errorMessage = "Het wachtwoord moet een hoofdletter bevatten!";
}

With:

if(password.ToLower() == password){
    errorMessage = "Het wachtwoord moet een hoofdletter bevatten!";
}   

If there is an upper case letter in it the two strings can never be the same.

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

1 Comment

At first I was like, that doesn't answer the question at all... second glance, you sneaky coder you.
1
if(!userName.Any(char.IsUpper)){
    errorMessage = "Het wachtwoord moet een hoofdletter bevatten!";
}

You'll need System.Linq

Comments

0

Sounds like in password is valid only when it contains a Uppercase character

then it is better to use LINQ

Eample :

string val = "aaabbb";
bool val1= val.Any(c => char.IsUpper(c));

val1 will be false here

string val = "aaabbbX";
bool val1= val.Any(c => char.IsUpper(c));

val1 will be true here

if you want to check string contains lower and upper case then

string val = "aaabbbX";
if (val.Any(char.IsUpper) && val.Any(char.IsLower))
{
// do your code here 
}

Comments

0

Use this function to find if the Character is a Upper Case or Lower Case

using System;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string inputString = "Welcome !!!";
            foreach(char input in inputString)
            {
                if(Char.IsLower(input))
                    Console.WriteLine(input + " is a Lower Case Character");
                else if (Char.IsUpper(input))
                    Console.WriteLine(input + " is a upper Case Character");

            }

            Console.Read();
        }
    }
}

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.