This post contains a total of 7+ CPP Password Validator Program Examples with Source Code. All these programs to Validate Passwords are made using C++.
You can use the source code of these examples with credits to the original owner.
Related Posts
CPP Password Validator Programs
1. By ROHIT KANOJIYA
Made by ROHIT KANOJIYA. Simple Password Validator. Source
Your Password: qwerty INVALID PASSWORD. Must Contain Atleast: 1 Alphabet 1 Digit 1 Special Character
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char s[10];
int spec=0,alpha=0,digit=0;
int i,len;
//cout<<"Enter your Password:";
cin>>s;
len=strlen(s);
cout<<"Your Password: "<<s;
if(len>=5 && len<=10)
{
for(i=0;i<len;i++)
{
if((s[i]>=65 && s[i]<=90) || (s[i]>=97 && s[i]<=122))
alpha++;
if(s[i]>=48 && s[i]<=57)
digit++;
if((s[i]>=91 && s[i]<=96) || (s[i]>=33 && s[i]<=47) || s[i]==64)
spec++;
}
if(spec && alpha && digit)
cout<<"\nVALID PASSWORD.";
else
cout<<"\nINVALID PASSWORD. \nMust Contain Atleast: \n1 Alphabet \n1 Digit \n1 Special Character";
}
else
{
if(len<5)
cout<<"\nINVALID PASSWORD.\nThe Length of Password is Less than Specified Length.\n";
if(len>10)
cout<<"\nINVALID PASSWORD.\nThe Length of Password is Greater than Specified Length.\n";
}
return 0;
}
2. By Zodiac
Made by Zodiac. Basic C++ Password Validator Program. Source
Passwor12## Password has met all the requirements.
#include <iostream>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int main() {
char password[10];
bool hasChar = false;
bool hasNumeric = false;
bool hasSymbols = false;
cin >> password;
//Check first for spaces
int counter = 0;
while(counter < 10)
{
if(password[counter]=='\0')
break;
if( !isgraph(password[counter]) )
{
cout << "Contains space." << endl;
exit(0);
}
counter++;
}
int passwordLength = strlen(password);
if( (passwordLength < 5) || (passwordLength > 10) )
{
cout << "Password does not meet size requirements." << endl;
exit(1);
}
counter = 0;
while(counter < passwordLength)
{
if( isdigit(password[counter]) )
{
hasNumeric = true;
counter++;
continue;
}
if( isalpha(password[counter]) )
{
hasChar = true;
counter++;
continue;
}
if( isgraph(password[counter]) )
{
hasSymbols = true;
counter++;
continue;
}
counter++;
}
if(hasChar && hasSymbols && hasNumeric)
cout << "Password has met all the requirements." << endl;
if(!hasChar)
cout << "Does not contain character." << endl;
if(!hasNumeric)
cout << "Does not contain any numbers." << endl;
if(!hasSymbols)
cout << "Does not contain any symbol." << endl;
return 0;
}
3. By ChillPill
Made by ChillPill. Password validator using regex. Source
12345.asdfg test succeeded. 1234.asdfg test succeeded. 123asd. test succeeded. a.12 test succeeded. asfgbk1234rtyyyuu77... test succeeded. ab.12 test succeeded. abc123 test succeeded. abc123. test succeeded. tests completed
#include <iostream>
#include <regex>
#include <string>
#include <cassert>
#include <unordered_map>
using namespace std;
bool validate(const string& s);
int main() {
unordered_map<string,bool> tests={
{"abc123.",true},
{"abc123",false},
{"asfgbk1234rtyyyuu77...",false},
{"ab.12",true},
{"a.12",false},
{"123asd.",true},
{"1234.asdfg",true},
{"12345.asdfg",false},
};
for(auto test:tests){
assert(validate(test.first)==test.second);
printf("%-22s test succeeded.\n",
test.first.c_str());
}
cout<<"\ntests completed";
return 0;
}
/*
password must have :
-one letter,
-one number,
-one special character,
-5-10 characters long
*/
bool validate(const string& s){
const regex length=regex("^.{5,10}$"),
oneChar=regex("^.*[A-Za-z].*$"),
oneDig=regex("^.*\\d.*$"),
oneSpecial=regex("^.*[^\\w].*$");
return (regex_match(s,length)&&
regex_match(s,oneChar)&&
regex_match(s,oneDig)&&
regex_match(s,oneSpecial));
}
4. By Shams Awais
Made by Shams Awais. Source
Enter your password:qwerty The password should contain at least one special character and at least one Number
#include <iostream>
#include<cstring>
using namespace std;
int main() {
char pass[50];
int no=0;//for counting numbers
int sp=0;//for counting special character
char c;
int i=0;//for traversing
cout<<"Enter your password:";
cin>>pass;
cout<<pass;
int len=strlen(pass);
c=pass[0];
if(len>=5&&len<=10)
{
while(c!='\0')
{
if(c>=48&&c<=57)
no++;
if((c>=33&&c<=47)||(c>=58&&c<=64)||(c>=91&&c<=96)||(c>=123&&c<=126))
sp++;
i++;
c=pass[i];
}
if(no<1||sp<1)
cout<<"\n The password should contain at least one special character and at least one Number";
else
cout<<"\nTrue";
}
else{
cout<<"\nLength of password should be in 5-10 range";
}
return 0;
}
5. By noaavra
Made by noaavra. Source
Please enter a password: Pass123!!! Your password is valid :)
#include <iostream>
using namespace std;
int main() {
const int MAX_SIZE = 10;
const int MIN_SIZE = 5;
char Password[10];
int i; // Counter in for loop
bool ContainNum = false;
bool ContainSpecial = false;
bool ContainSpace = false;
cout << "Please enter a password:" << endl;
cin >> Password;
// Check every elemnt in the array, until its end
for (i = 0; Password[i] != '\0'; i++)
{
// If current element is number
if ((Password[i] >= 48) && (Password[i] <= 57))
{
ContainNum = true;
}
// If current element is special character
else if (((Password[i] >= 33) && (Password[i] <= 47)) ||
((Password[i] >= 58) && (Password[i] <= 64)) ||
((Password[i] >= 91) && (Password[i] <= 96)) ||
((Password[i] >= 123) && (Password[i] <= 126)))
{
ContainSpecial = true;
}
// If current element is space
else if (Password[i] == 32)
{
ContainSpace = true;
break;
}
}
// Password must have at least 1 number, at least 1 special character, no spaces,
// and be in the right length
if ((i >= MIN_SIZE) && (i <= MAX_SIZE) &&
(ContainNum == true) && (ContainSpecial == true) &&
(ContainSpace == false))
{
cout << "Your password is valid :)" << endl;
}
else
{
cout << "Your password is not valid!" << endl;
}
return 0;
}
6. By Ritwik
Made by Ritwik. Source
Pass Weak
#include <iostream>
#include <string>
using namespace std;
int main()
{
string pass;
int size,a=0,b=0,i;
getline(cin,pass);
size=pass.size();
if(size<7)
{
cout << "Weak";
return 0;
}
for(i=0;i<size;i++)
{
{
if(pass[i]=='!'|| pass[i]=='@'||pass[i]=='#'||pass[i]=='%'||pass[i]=='$'||pass[i]=='&'||pass[i]== '*')
++a;
}
{
if( pass[i]=='1'||pass[i]=='2'||pass[i]=='3'||pass[i]=='4'||pass[i]=='5'||pass[i]=='6'||pass[i]=='7' ||pass[i]=='8'||pass[i]=='9'||pass[i]=='0')
++b;
}
}
if(a>=2&&b>=2)
cout << "Strong" ;
else
cout << "Weak";
return 0;
}
7. By Kinshuk Vasisht
Made by Kinshuk Vasisht. Source
Pass123### Valid
#include<iostream>
#include<string>
#include<cstring>
#include<cctype>
#define elif else if
using namespace std;
bool ValidPWD(string pw)
{
if(pw.length()<8||pw.length()>12)
return false;
else
{
int nc=0, cc=0 , ac=0;
char* pwc=new char[pw.length()];
strcpy(pwc,pw.c_str());
for(int i=0;i<pw.length();i++)
{
if(isdigit(pwc[i]))
nc++;
elif(isalpha(pwc[i]))
{
ac++;
if(isupper(pwc[i]))
cc++;
}
if(pwc[i]==' '||pwc[i]\
=='\\'||pwc[i]=='/')
return false;
}
if(ac==0||nc==0||cc==0\
||(isdigit(pwc[0])))
return false;
return true;
}
}
int main(void)
{
string pwd;
getline(cin,pwd);
if(ValidPWD(pwd))
cout<<"Valid";
else
cout<<"Invalid";
}
8. By Dhairya
Made by Dhairya. Source
Passwo Password too short/long!
#include <iostream>
#include <string.h>
using namespace std;
int main() {
bool y;
y=false;
typedef char str[15];
str a;
cin.getline(a,15);
int l =strlen(a);
if(l>=5 && l<=10)
{
int c=0,n=0;
for(int i=0;i<l;i++)
{
if(a[i]>='0' && a[i]<='9')
{
n++;
continue;
}
y=((a[i]>='a' && a[i]<='z') || (a[i]>='A' && a[i]<='Z'));
if (y==false)
{
c++;
}
if (a[i]==' ')
{
c=0;
n=0;
break;}
}
if(n>0 && c>0)
cout<<"Password is valid!";
else
{
if (n==0)
cout<<"Password doesn't have any digits!";
else
cout<<"password doesn't have any special character!";
}
}
else
cout<<"Password too short/long!";
return 0;
}