-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidation_using_regex.java
More file actions
109 lines (82 loc) · 3.57 KB
/
Copy pathValidation_using_regex.java
File metadata and controls
109 lines (82 loc) · 3.57 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package Assignment_2;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Validation_using_regex {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice;
do {
System.out.println("\n--- REGEX VALIDATION MENU ---");
System.out.println("1. Validate Mobile Number");
System.out.println("2. Validate Email ID");
System.out.println("3. Validate Username");
System.out.println("4. Validate Password");
System.out.println("5. Exit");
try {
choice = sc.nextInt();
sc.nextLine(); // consume newline
switch (choice) {
case 1: {
System.out.println("Enter Mobile Number:");
String Mobilenum = sc.nextLine();
Pattern p = Pattern.compile("^[6-9][0-9]{9}$");
Matcher m = p.matcher(Mobilenum);
if (m.matches()) {
System.out.println("Welcome! Mobile Number is Valid");
} else {
System.out.println("Invalid Mobile Number");
}
break;
}
case 2: {
System.out.println("Enter Email ID:");
String email = sc.nextLine();
Pattern p = Pattern.compile("^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$");
Matcher m = p.matcher(email);
if (m.matches()) {
System.out.println("Welcome! Email ID is Valid");
} else {
System.out.println("Invalid Email ID");
}
break;
}
case 3: {
System.out.println("Enter Username:");
String username = sc.nextLine();
Pattern p = Pattern.compile("^[A-Za-z][A-Za-z0-9_]{4,14}$");
Matcher m = p.matcher(username);
if (m.matches()) {
System.out.println("Welcome! Username is Valid");
} else {
System.out.println("Invalid Username");
}
break;
}
case 4: {
System.out.println("Enter Password:");
String password = sc.nextLine();
Pattern p = Pattern.compile("^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[@#$%]).{8,}$");
Matcher m = p.matcher(password);
if (m.matches()) {
System.out.println("Welcome! Password is Strong");
} else {
System.out.println("Invalid Password");
}
break;
}
case 5:
System.out.println("Exiting Program...");
break;
default:
System.out.println("Invalid Choice");
}
} catch (Exception e) {
System.out.println("Error: Invalid Input");
sc.nextLine(); // clear buffer
choice = 0;
}
} while (choice != 5);
sc.close();
}
}