-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.cpp
More file actions
55 lines (46 loc) · 1.55 KB
/
validation.cpp
File metadata and controls
55 lines (46 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// https://www.geeksforgeeks.org/how-to-validate-user-input-in-cpp/
#include <iostream>
#include <limits>
#include <string>
#include <regex>
using namespace std;
bool isValidPassword(const string& password) {
if (password.length() < 8) return false;
bool hasLower = false, hasUpper = false, hasDigit = false;
for (char ch : password) {
if (islower(ch)) hasLower = true;
if (isupper(ch)) hasUpper = true;
if (isdigit(ch)) hasDigit = true;
}
return hasLower && hasUpper && hasDigit;
}
int main() {
int number;
cout << "Enter an integer: ";
while (!(cin >> number)) {
cout << "Invalid input. Please enter an integer: ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "You entered: " << number << endl;
// ======================================
string email;
regex email_pattern(R"((\w+)(\.{1}\w+)*@(\w+)(\.{1}\w+)+)");
cout << "Enter an email address: ";
getline(cin, email);
if (!regex_match(email, email_pattern)) {
cerr << "Invalid email format." << endl;
} else {
cout << "Valid email: " << email << endl;
}
// ======================================
string password;
cout << "Enter a password (at least 8 characters, including uppercase, lowercase, and a digit): ";
cin >> password;
while (!isValidPassword(password)) {
cout << "Invalid password. Please enter a valid password: ";
cin >> password;
}
cout << "You entered a valid password." << endl;
return 0;
}