-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailValidation.java
More file actions
171 lines (140 loc) · 3.51 KB
/
EmailValidation.java
File metadata and controls
171 lines (140 loc) · 3.51 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Name = Rahul Navin
// Student id = 261062889
public class EmailValidation {
public static void main(String args[]) {
String emailID = args[0];
System.out.println("Email-ID : " + emailID);
System.out.println(" ");
if(isValidEmail(emailID)==true) {
System.out.println("Is Email-ID Valid : true");
}
else {
System.out.println("Is Email-ID valid : false");
}
}
public static boolean isAlphanumeric(char input) {
if((input >= 'a' && input <='z')||(input >='A' && input <='Z')||(input >='0' && input <= '9')){
return true;
}
else {
return false;
}
}
public static boolean isValidPrefixChar(char input) {
if(isAlphanumeric(input) == true || input =='-' || input =='_' || input == '.')
return true;
else
return false;
}
public static boolean isValidDomainChar(char input) {
if (isAlphanumeric(input) == true || input=='-' || input == '.') {
return true;
}
else {
return false;
}
}
public static boolean exactlyOneAt(String one)
{
int count = one.length()- one.replaceAll("@","").length();
if(count == 1)
{
return true;
}
else
{
return false;
}
}
public static String getPrefix(String s)
{
String st[] = s.split("@");
String prefix = st[0];
return prefix;
}
public static String getDomain(String s)
{
int i = s.indexOf("@");
String domain = s.substring(i+1);
return domain;
}
/* The below is the additional method to check domain contain atleast one period(".")
to divide domain into 2 Parts.*/
public static boolean atleastOnePeriodDomain(String s)
{
int count = s.length() - s.replaceAll("/.", "").length();
if(count >=1)
{
return true;
}
else
return false;
}
public static boolean isValidPrefix(String prefix) {
char arr[] = prefix.toCharArray();
int i = prefix.length();
char a = prefix.charAt(i-1);
if (isAlphanumeric(arr[0]) == true && prefix.matches(".*[a-zA-Z]+.*") && isAlphanumeric(a)==true)
{
for(char ch: arr)
{
if (isValidPrefixChar(ch) == true)
{
if(isValidPrefixChar(ch)==true && isAlphanumeric(ch)==false)
{
int j = prefix.indexOf(ch);
char c = prefix.charAt(j+1);
if (isAlphanumeric(c)==true)
{}
else
return false;
}
}
else
return false;
}
return true;
}
else
return false;
}
public static boolean isValidDomain(String domain) {
int i = domain.lastIndexOf(".");
String firstpart = domain.substring(0,i);
String endpart = domain.substring(i+1);
if (endpart.matches("^[a-zA-Z]+$") && endpart.length() >= 2 && firstpart.matches(".*[a-zA-Z]+.*")) {
char arr[] = firstpart.toCharArray();
for(char ch: arr)
{
if (isValidDomainChar(ch)==true)
{
if ( isValidDomainChar(ch)== true && isAlphanumeric(ch) == false)
{
int j = domain.indexOf(ch);
char c = domain.charAt(j+1);
if(isAlphanumeric(c) == true)
{}
else
return false;
}
}
else
return false;
}
return true;
}
else
return false;
}
public static boolean isValidEmail(String email) {
String prefix = getPrefix(email);
String domain = getDomain(email);
if(exactlyOneAt(email)== true && atleastOnePeriodDomain(domain)==true && isValidDomain(domain) == true
&& isValidPrefix(prefix)==true)
{
return true;
}
else
return false;
}
}