cancel
Showing results for 
Search instead for 
Did you mean: 

Email ID validation - UDF help required

former_member183906
Active Contributor
0 Kudos

Hi,

I have a field A -I have to validate where the field value of A is proper Email or not like 1@x.com,1@x.co.in etc...

can i have the UDF for it. Please share the java program for Email id validation of a field.

I have to use graphical mapping and UDF for it.

Rgds

Accepted Solutions (1)

Accepted Solutions (1)

anupam_ghosh2
Active Contributor
0 Kudos

Hi SAP PI,

                    Please try the following UDF

public static String validate(String emailid){

if(emailid.matches("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"))

        {

            return "validEmail";

        }   

        return "InvalidEmail";

    }

Output

ebay@yahoo.com is validEmail

asho_ty5@xyz.com is validEmail

took is InvalidEmail

ywook@@34.com is InvalidEmail

wonder-abc@yahoo.com is validEmail

abc.@t.com is InvalidEmail

Regards

Anupam

former_member183906
Active Contributor
0 Kudos

my input field A is string array.. so its not supporing matchesfunction and failing.

public void getEmail(ResultList result, String[] emailid, Container container) throws StreamTransformationException{

}

pls advise..

subhro_de
Active Participant
0 Kudos

Hi SAP PI,

Are you trying to check for multiple values for email validation and then return corresponding true / false.

In that case run a loop on the string array and call Baskar's or Anupam's method - one option can be:

for( int i = 0; i < emailid.length; i++)
{
   
String element = emailid[i];


try {
     
new InternetAddress(
element).validate();              
      result.addValue("true");
  
} catch (AddressException ex) {
     
result.addValue("false");
  
}
  
}

Former Member
0 Kudos

Take Input as String not String Array then use above function .

anupam_ghosh2
Active Contributor
0 Kudos

Hi SAP PI,

                       Just try this code

public  void validate(String emailid[],ResultList result,Container container)throws StreamTransformationException{

        for(int i=0;i<emailid.length;++i)

        {

            if(emailid[i].matches("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"))

            {

                result.addValue("validEmail");

            }

            else

            {

                result.addValue("InvalidEmail");

            }

        }   

        return;

    }

Regards

Anupam

Answers (3)

Answers (3)

former_member183906
Active Contributor
0 Kudos

I am getting error in UDF -

            Cannot cast '1@X.COM' to boolean

  any advise..

praveen_sutra
Active Contributor
0 Kudos

what is the UDF you are using ? Could you please paste the UDF .

thanks and regards,

Praveen T

baskar_gopalakrishnan2
Active Contributor
0 Kudos

It's easy. Use this link

http://stackoverflow.com/questions/13077897/java-email-address-validation

import javax.mail.internet.AddressException;

import javax.mail.internet.InternetAddress;

public boolean isValidEmailAddress(String email) {
  
try {
     
new InternetAddress(email).validate();
  
} catch (AddressException ex) {
     
return false;
  
}
  
return true;
}

use this link for reference

http://docs.oracle.com/javaee/1.4/api/javax/mail/internet/InternetAddress.html

nitinlpatil12
Participant
0 Kudos

Check this program may be helpful.

package com.java2novice.regex;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class MyEmailValidate {

    private static Pattern emailNamePtrn = Pattern.compile(

    "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");

   

    public static boolean validateEmailAddress(String userName){

       

        Matcher mtch = emailNamePtrn.matcher(userName);

        if(mtch.matches()){

            return true;

        }

        return false;

    }

Regards,

Nitin Patil