Wednesday, June 3, 2015

Java - Check If Email is Valid by Checking the HostName

//java
   public String checkHostName(){  
     try{  
       String email = request.getParameter("email");       
       Integer index = email.indexOf('@');  
       String domain = email.substring(index + 1, email.length());  
       /*check if domain exists in dns*/  
       try{  
         InetAddress inetAddress = InetAddress.getLocalHost();  
         inetAddress = InetAddress.getByName(domain);  
         host = inetAddress.getHostName().toString();          
       }catch(UnknownHostException e){  
         host = "unknown";  
       }  
       return SUCCESS;  
     }catch(Exception e){  
       e.printStackTrace();  
       return ERROR;  
     }  
   }  

//jquery
 function checkHostName(callback){  
   var host = null;  
   var email = $('#email').val();          
   $.ajax({  
    type: "POST",  
    url: "checkhostname.html",     
    async:false,  
    data: {  
       email: email  
    },  
    success: callback  
   });  
   return host;  
 }  
//call jquery using jquery callback
 checkHostName(function(host){          
           $('#hostName').val(host);   
           if(host == 'unknown'){               
             $('#erremail').text('Invalid email address');              
           }  
           else{ $('#erremail').text('');  
           }  
         });   

No comments:

Post a Comment