Introduction
Validating user input is an important concept in programming, especially for login and registration systems. One common task is checking whether an email (Gmail) is valid.
In this blog, we will learn how to validate a Gmail address using:
- Python
- Java
And the important part: without using advanced predefined methods like regex
Problem Statement
We need to check:
- Only valid characters are used
- Email ends with @gmail.com
Python Implementation
```python id="8y2b2a"
def verify_Gmail(Gmail):
i = 0
while i < len(Gmail):
current = Gmail[i]
i += 1
if current >= "a" and current <= "z":
continue
elif current >= "A" and current <= "Z":
continue
elif current >= "0" and current <= "9":
continue
elif current == "@" or current == "." or current == "_":
continue
else:
return False
if Gmail[-10:] == "@gmail.com":
return True
else:
return False
if verify_Gmail(input("Enter Your Gmail :")):
print("valid")
else:
print("invalid")
---
## Java Implementation
```java id="0m5x2r"
import java.util.*;
public class GmailValidation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Your Gmail: ");
String Gmail = sc.nextLine();
if (verifyGmail(Gmail)) {
System.out.println("valid");
} else {
System.out.println("invalid");
}
sc.close();
}
public static boolean verifyGmail(String Gmail) {
// Check each character
for (int i = 0; i < Gmail.length(); i++) {
char current = Gmail.charAt(i);
if (current >= 'a' && current <= 'z') {
continue;
} else if (current >= 'A' && current <= 'Z') {
continue;
} else if (current >= '0' && current <= '9') {
continue;
} else if (current == '@' || current == '.' || current == '_') {
continue;
} else {
return false;
}
}
// Check ending "@gmail.com"
if (Gmail.length() >= 10) {
String lastPart = Gmail.substring(Gmail.length() - 10);
if (lastPart.equals("@gmail.com")) {
return true;
}
}
return false;
}
}
Logic Explanation
Step 1: Loop through each character
We check every character using:
- Python →
while loop - Java →
for loop
Step 2: Validate allowed characters
Allowed:
- Alphabets (a–z, A–Z)
- Numbers (0–9)
- Special characters:
@,.,_
If any other character appears → invalid
Step 3: Check Gmail domain
We ensure the email ends with:
```text id="bh84jm"
@gmail.com
---
## Valid Examples
* `ponvel123@gmail.com`
* `user_name@gmail.com`
---
## Invalid Examples
* `ponvel@gmail`
* `ponvel#@gmail.com`
* `user@gmail.org`
---
## Limitations
* Does not check multiple `@` symbols
* Does not fully validate email format
* Basic validation only
Top comments (0)