Program to check valid username and password

Program to check valid username and password

  • Write a program to check valid username and password in C
  • Write a program to check valid username and password in C++
  • Write a program to check valid username and password in Python
  • Write a program to check valid username and password in PHP
  • Write a program to check valid username and password in Java
  • Write a program to check valid username and password in Java Script
  • Write a program to check valid username and password in C#

Explanation:

We must establish certain standards for each in order to verify the legitimacy of a username and password. These guidelines usually entail verifying that the password and username adhere to particular format, character, and length specifications. The general reasoning for confirming a login and password is as follows:

Logic for Validating Username and Password

Username Validation:

  1. Length Check: Ensure the username is within an acceptable length (e.g., between 3 to 20 characters).
  2. Character Check: The username should only contain alphanumeric characters (letters and digits), and sometimes may allow special characters like underscores (_).
  3. No Spaces: The username should not contain spaces.

Password Validation:

  1. Length Check: Ensure the password meets a minimum length (e.g., at least 8 characters).
  2. Complexity Check:
    • At least one uppercase letter.
    • At least one lowercase letter.
    • At least one digit.
    • At least one special character (e.g., @, #, $, etc.).
  3. No Spaces: Password should not contain spaces.

Program to check valid username and password

#include <stdio.h>
#include <string.h>

int main() {
    char username[50], password[50];
    const char validUsername[] = "admin";
    const char validPassword[] = "12345";

    printf("Enter username: ");
    scanf("%s", username);

    printf("Enter password: ");
    scanf("%s", password);

    if (strcmp(username, validUsername) == 0 && strcmp(password, validPassword) == 0) {
        printf("Login successful!\n");
    } else {
        printf("Invalid username or password.\n");
    }

    return 0;
}

#include <iostream>
#include <string>
using namespace std;

int main() {
    string username, password;
    const string validUsername = "admin";
    const string validPassword = "12345";

    cout << "Enter username: ";
    cin >> username;

    cout << "Enter password: ";
    cin >> password;

    if (username == validUsername && password == validPassword) {
        cout << "Login successful!" << endl;
    } else {
        cout << "Invalid username or password." << endl;
    }

    return 0;
}

valid_username = "admin"
valid_password = "12345"

username = input("Enter username: ")
password = input("Enter password: ")

if username == valid_username and password == valid_password:
    print("Login successful!")
else:
    print("Invalid username or password.")

<?php
$validUsername = "admin";
$validPassword = "12345";

echo "Enter username: ";
$username = trim(fgets(STDIN));

echo "Enter password: ";
$password = trim(fgets(STDIN));

if ($username === $validUsername && $password === $validPassword) {
    echo "Login successful!\n";
} else {
    echo "Invalid username or password.\n";
}
?>

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        final String validUsername = "admin";
        final String validPassword = "12345";

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter username: ");
        String username = scanner.nextLine();

        System.out.print("Enter password: ");
        String password = scanner.nextLine();

        if (username.equals(validUsername) && password.equals(validPassword)) {
            System.out.println("Login successful!");
        } else {
            System.out.println("Invalid username or password.");
        }
    }
}

const validUsername = "admin";
const validPassword = "12345";

const username = prompt("Enter username:");
const password = prompt("Enter password:");

if (username === validUsername && password === validPassword) {
    console.log("Login successful!");
} else {
    console.log("Invalid username or password.");
}

using System;

class Program {
    static void Main() {
        const string validUsername = "admin";
        const string validPassword = "12345";

        Console.Write("Enter username: ");
        string username = Console.ReadLine();

        Console.Write("Enter password: ");
        string password = Console.ReadLine();

        if (username == validUsername && password == validPassword) {
            Console.WriteLine("Login successful!");
        } else {
            Console.WriteLine("Invalid username or password.");
        }
    }
}

List of All Programs