This java program accepts an email address as a user input and it can validate and give a particular error message if the email is invalid.
import java.io.*; public class EmailValidate { public static void main(String args[]) throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String email; boolean status = true; System.out.print("Enter your email address : "); email = br.readLine(); char firstCharacter = email.charAt(0); if( ! Character.isLetter(firstCharacter)) { System.out.println("Invalid Email ! First character must be a letter !"); status = false; } if(email.indexOf('@') < 0) { System.out.println("Invalid Email ! '@' is not present !!!"); status = false; } else if( email.indexOf('@') != email.lastIndexOf('@') ) { System.out.println("Cannot Have more than one '@' characters !!!"); status = false; } if(email.indexOf('.') < 0) { System.out.println("Invalid Email ! '.' is not present !!!"); status = false; } else if( email.indexOf('.') != email.lastIndexOf('.') ) { System.out.println("Cannot Have more than one '.' characters !!!"); status = false; } if((email.indexOf('@') > email.indexOf('.')) & status) { System.out.println("Invalid Email ! '@' should come before '.' "); status = false; } int indexAt = email.indexOf('@'); int indexDot = email.indexOf('.'); int length = email.length(); if(indexDot == indexAt + 1) { System.out.println("No service provider spacefied ! Invalid email !"); status = false; } if(indexDot+2 > length) { System.out.println("No Domain name specified ! Invalid email !"); status = false; } if(status == true) { char userName [] = new char[indexAt]; char service [] = new char[indexDot - indexAt - 1]; char domain [] = new char[length - indexDot - 1]; for(int iuser = 0; iuser< indexAt; iuser++) userName[iuser] = email.charAt(iuser); int slimit = 0; for(int iservice = indexAt+1; iservice< indexDot; iservice++) { service[slimit] = email.charAt(iservice); slimit++; } int dlimit = 0; for(int idomain = indexDot+1; idomain< 1) { System.out.println("Invalid username !"); status = false; } if(service.length < 1) { System.out.println("Invalid service provider !"); status = false; } if(domain.length < 1) { System.out.println("Invalid Domain name !"); status = false; } } if(status == true) System.out.println("Valid Email. Successfully Validated."); } }
-Tharindu Edirisinghe-
-SLIIT 10'-
0 comments:
Post a Comment