Question

Email validation on property

Issue

Pega provides multiple email validations OOTB
Edit Validate rule: ValidEmailAddress
Function: pyServicesUtilities • pyValidateEmailID

Email validation on property
OOTB Edit Validate: ValidEmailAddress
null: true
name: false
name@: false
name@domain: true
name@domain.com: true

Email validation via Validate rule
OOTB Function: pyServicesUtilities • pyValidateEmailID
[expression evaluates to true]
!@(Pega-IntegrationArchitect:pyServicesUtilities).pyValidateEmailID(.EmailProperty)
null: false
name: false
name@: false
name@domain: false
name@domain.com: true

Using the regex java code of the Function(pyServicesUtilities • pyValidateEmailID) in the Edit Validate(ValidEmailAddress) should result in validation on property:
null: false
name: false
name@: false
name@domain: false
name@domain.com: true

However, it results in:
null: true
name: false
name@: false
name@domain: true
name@domain.com: true

***Edited by Moderator: Lochan to update platform capability tags***

Correct Answer
October 22, 2019 - 9:15am

Hi,

The OOTB 'ValidEmailAddress' validation rule will validate only the format " xxx@xxx "

We need to perform customization based on business requirement. I have tried the below customization at my end,

Create a EditValidate rule with the below code,

if (theValue == null || theValue.trim().equals("")) return false;
try {
            javax.mail.internet.InternetAddress emailAddr = new javax.mail.internet.InternetAddress(theValue);
            emailAddr.validate();
 }
catch (javax.mail.internet.AddressException ex){
   return false;
}
if(theValue.contains("."))
    return true; 
theProperty.addMessage("Enter a Valid Email Address");
return false;

For client side validation, use 'Post Value' for on-change event of the property outside the grid or add the below script in UserWorkForm,

<script>
var ruleEditValidate_validEmail = new validation_ValidationType("validMail", ruleEditValidate_isValidEmail);
    ruleEditValidate_validEmail.addEventFunction("onchange", ruleEditValidate_isValidEmail);
  function ruleEditValidate_isValidEmail(object) {

var x = pega.control.PlaceHolder.getValue(object);

    if (null == x || "" == x) {
        return;
    }

    // Regular expression for email address
    // RFC822 regular expression

    var emailReg = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]+$/;

    var validMail = emailReg.test(x);

    if (!validMail) {
        return display_getValidationError(object, ruleEditValidate_isValidEmailAddressMsgStr, "", true);
    }
}
  </script>

Note: ValidMail (Mentioned in above script) is the custom edit validate rule created.

When I use the above customization, it validates the email address and throws error if the entered value is not in the format " xxx@xx.xx "

Hope it helps!

Thanks,

Geeda.

Comments

Keep up to date on this post and subscribe to comments

October 22, 2019 - 7:52am

Which platform version are you using?  I just ran a test in 8.1.5 using the pyValidateEmailID function and it worked as expected.

October 22, 2019 - 8:24am
Response to Marc Alderman

8.1.5

Pega
October 22, 2019 - 9:15am
Response to BobReawaruw

Hi,

The OOTB 'ValidEmailAddress' validation rule will validate only the format " xxx@xxx "

We need to perform customization based on business requirement. I have tried the below customization at my end,

Create a EditValidate rule with the below code,

if (theValue == null || theValue.trim().equals("")) return false;
try {
            javax.mail.internet.InternetAddress emailAddr = new javax.mail.internet.InternetAddress(theValue);
            emailAddr.validate();
 }
catch (javax.mail.internet.AddressException ex){
   return false;
}
if(theValue.contains("."))
    return true; 
theProperty.addMessage("Enter a Valid Email Address");
return false;

For client side validation, use 'Post Value' for on-change event of the property outside the grid or add the below script in UserWorkForm,

<script>
var ruleEditValidate_validEmail = new validation_ValidationType("validMail", ruleEditValidate_isValidEmail);
    ruleEditValidate_validEmail.addEventFunction("onchange", ruleEditValidate_isValidEmail);
  function ruleEditValidate_isValidEmail(object) {

var x = pega.control.PlaceHolder.getValue(object);

    if (null == x || "" == x) {
        return;
    }

    // Regular expression for email address
    // RFC822 regular expression

    var emailReg = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]+$/;

    var validMail = emailReg.test(x);

    if (!validMail) {
        return display_getValidationError(object, ruleEditValidate_isValidEmailAddressMsgStr, "", true);
    }
}
  </script>

Note: ValidMail (Mentioned in above script) is the custom edit validate rule created.

When I use the above customization, it validates the email address and throws error if the entered value is not in the format " xxx@xx.xx "

Hope it helps!

Thanks,

Geeda.