SAP Home Learn Build Integrate Model Operate Extend with AI ConnectTutorial navigator Knowledge Graph API Devtoberfest Developer Advocates App Space

Manage my Account SAP Devs YouTube ↗ Learnings ↗ Community ↗ Provide Feedback ↗
Logout
โคข Open full site

Define a Validation Rule in an MDK App

Write a JavaScript logic to validate an email address format in an MDK app.

Overview

🎓 beginner 10 min. Mobile Development Kit ClientBeginnerIosAndroidMobileSAP Business Technology PlatformSAP Mobile ServicesSAP Build CodeSAP BuildSAP Business Application Studio

You will learn

  • โœ”How to write a rule to validate an UI field
Jitendra Kansal J Jitendra Kansal July 9, 2026
Created by October 4, 2022
Contributors

Prerequisites

Steps

Intro

When allowing end-users to make updates to data, it is important to add validation rules to verify that they are entering valid information. If the Update action fails due to the validation rule, the application will display a validation failure message to the end-user.

MDK
MDK

Step 1 Add a Validation Rule to the Customer Update action
โ€”

You will add a rule to the Update action to run the validation before saving any data. If the validation rule is successful, the Update action will save the changes as expected. If the validation rule fails, the end-user receives the validation failure message telling them useful information so they can fix the problem before continuing.

  1. Open Customers_UpdateEntity.action by double clicking on the action in the project explorer pane.

  2. Expand the Common Action Properties and click the Create a rule icon to create a new Validation Rule.

    MDK
    MDK

  3. Keep the default selection for the Object Type as Rule and Folders path. Click OK.

    MDK
    MDK

  4. In the Base Information step, enter the Rule Name as EmailValidation and then click Finish.

    MDK
    MDK

    You can find more details about writing a Rule.

  5. Replace the generated snippet with below code.

    JavaScript
    /**
    * Describe this function...
    * @param {IClientAPI} context
    */
    export default function EmailValidation(context) {
        //The following evaluateTargetPath will retrieve the current value of the email control
        if ((context.evaluateTargetPath('#Control:FCEmail/#Value').indexOf('@')) === -1) {
            //If email value does not contain @ display a validation failure message to the end-user
            context.executeAction('/demosampleapp/Actions/ValidationFailure.action');
        } else {
            //If @ is present in the email value, return true to indicate validation is successful
            return true;
        }
    }

    This rule will handle validation if a @ symbol exists in the email address. In this validation rule, you will grab the data entered by the end-user, validate it and check for the @ symbol then return true if the email address is of a valid format or false if it is not. The returning result of the validation rule can be used in the Update action to determine whether the action succeeds or fails.

    The indexOf() method returns the index within the calling String object of the first occurrence of the specified value and -1, if no occurrence is found.

    In above code there is a reference to ValidationFailure.action, which doesn’t yet exist in your metadata project. You will create this action in next step.

  6. In the generated EmailValidation.js rule, double-click the red line. You will notice a bulb icon suggesting some fixes, click on it, select MDK: Create action for this reference, and click Message.

    MDK
    MDK

  7. Provide the below information in the ValidationFailure.action:

    FieldValue
    MessageEmail address is not in the correct format recipient @ domain . domaintype
    TitleValidate Email
    OKCaptionOK
    OnOK--None--
    CancelCaptionleave it blank
    OnCancel--None--

    MDK
    MDK

Step 2 Deploy the Project
+
Step 3 Run the Project
+

Resources

Discussion

Share feedback on this tutorial or join the conversation in SAP Community.

Submit detailed feedback Discuss in Community
Steps
Step 1 of 3
1. Add a Validation Rule to the Customer Update action 2. Deploy the Project 3. Run the Project

Learn more →