Email Validation Script

Summary

Email Validator Program developed using pure Object Oriented Programming & String handling in Java during college days as an assignment.

Ability of Validating

Username

_rohan@gmail.com INVALID
.rohan@gmail.com INVALID
rohan$sakhale@gmail.com INVALID
rohan^sakhale@gmail.com INVALID
rohan.@gmail.com INVALID
12rohan@gmail.com INVALID

rohansakhale@gmail.com VALID
rohan_sakhale@gmail.com VALID
rohan.sakhale@gmail.com VALID
rohan123sakhale@gmail.com VALID

Special character “@”

rohan@sakhale@gmail.com INVALID
rohan@@gmail.com INVALID
@@gmail.com INVALID
rohan@gmail.com VALID

Domain Name

rohan@.gmail.com INVALID
rohan@a.com INVALID
rohan@aaa.123 INVALID
rohan@aa.co.com.in INVALID
rohan@-gmail.com INVALID
rohan@gmail_gmail.com INVALID

rohan@gmail.com VALID
rohan@gmail.co.in VALID
rohan@123.comVALID
rohan@gmail.in VALID
rohan@rohan-rohan.com VALID

Code

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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package emailverifieroop;
import java.io.*;
/**
*
* @author RohanSakhale
*/
public class EmailVerifier {
String s;
public EmailVerifier() {
s = "";
}
public EmailVerifier(String s) {
this.s = s;
}
void setEmail(String email) {
this.s = email;
}
/**
* Accept string from user
*/
void accept() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
s = br.readLine();
br.close();
}
/**
* Display string entered
*/
void display() {
System.out.println("You have entered: " + s);
}
/**
* Validate username from email
*/
boolean checkUsername() {
if (s.length() >= 3) {
for (int i = 0; i < s.indexOf("@"); i++) {
if (i != 0 && (s.charAt(i) == '_' || s.charAt(i) == '.')) {
continue;
}
if ((int) s.charAt(i) < 47 && (int) s.charAt(i) > 58) {
if (s.startsWith(Integer.toString(i)) == true) {
return false;
}
}
if ((int) s.charAt(i) < 47) {
return false;
} else if ((int) s.charAt(i) > 57 && (int) s.charAt(i) < 65) {
return false;
} else if ((int) s.charAt(i) > 91 && (int) s.charAt(i) < 97) {
return false;
} else if ((int) s.charAt(i) > 123) {
return false;
}
}
return true;
} else {
return false;
}
}
/**
* Validate email username
*/
boolean checkEmailUsername() {
if (s.indexOf("@") >= 3 && s.charAt(s.indexOf("@") - 1) != '.' && s.charAt(s.indexOf("@") - 1) != '_') {
for (int i = 0; i < s.indexOf("@"); i++) {
if (i != 0 && (s.charAt(i) == '_' || s.charAt(i) == '.')) {
continue;
}
if ((int) s.charAt(i) < 47 && (int) s.charAt(i) > 58) {
if (s.startsWith(Integer.toString(i)) == true) {
return false;
}
}
if ((int) s.charAt(i) < 47) {
return false;
} else if ((int) s.charAt(i) > 57 && (int) s.charAt(i) < 65) {
return false;
} else if ((int) s.charAt(i) > 91 && (int) s.charAt(i) < 97) {
return false;
} else if ((int) s.charAt(i) > 123) {
return false;
}
}
return true;
} else {
return false;
}
}
/**
* Validate "@" exists
*/
boolean checkPresenceOfSymbol() {
if (s.indexOf("@") == -1) {
return false;
} else {
return true;
}
}
/**
* Validate "@" is not the last character of string
*/
boolean checkEmailSymbol() {
if (s.indexOf("@") == s.lastIndexOf("@")) {
return true;
} else {
return false;
}
}
/**
* Validate the domain name
*/
boolean checkDomainName() {
int count = 0;
if (s.charAt(s.indexOf("@") + 1) == '_' || s.charAt(s.indexOf("@") + 1) == '-') {
return false;
}
if (s.indexOf(".", s.indexOf("@")) - s.indexOf("@") >= 3) {
for (int i = s.indexOf("@") + 1; i < s.length(); i++) {
if (s.charAt(i) == '.') {
count++;
if (count > 2) {
return false;
}
}
}
for (int i = s.indexOf("@") + 1; i < s.length(); i++) {
if ((s.charAt(i) == '.' || s.charAt(i) == '-')
|| s.indexOf("-") - s.indexOf("@") > 1) {
continue;
}
if ((int) s.charAt(i) < 47) {
return false;
}
if ((int) s.charAt(i) > 47 && (int) s.charAt(i) < 57) {
continue;
}
if ((int) s.charAt(i) < 97) {
return false;
}
if ((int) s.charAt(i) > 123) {
return false;
}
}
if (s.lastIndexOf(".") == s.length() - 4) {
return true;
} else if (s.lastIndexOf(".") == s.length() - 3) {
return true;
} else if (s.indexOf(".", s.indexOf("@")) == s.length() - 2 && s.lastIndexOf(".") == s.length() - 5) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* Validate string as email
*/
boolean checkEmail() {
if (!checkEmailUsername()) {
return false;
}
if (!checkPresenceOfSymbol()) {
return false;
}
if (!checkDomainName()) {
return false;
}
if (!checkEmailSymbol()) {
return false;
}
return true;
}
}