How to restrict the end user to enter the duplicate emails or mobile?

Certified Associate Developer

Hi all,

I am working on a project, I want to restrict the end user to enter the duplicate email ids or mobile numbers. I mean, end user cannot enter the email or mobile number if it's already exist in the Database.

It should show an error message if user entered existed email id on the interface.

Solutions would be highly appreciated.

Thank you in advance!

  Discussion posts and replies are publicly visible

  • Do you want to see if this user has already entered the same phone number / email or any user? If it's only for this user, it's probably easiest to query on loading the form to return all of this user's existing emails / phone numbers. Then, you could do a comparison in a validation to check if the user has already entered this value or not.

    If you're looking to see if this phone number / email is unique across all of your data, you should likely query after the user has entered the data to check and see if there are any rows in the database with that exact email / phone number.

  • 0
    Certified Lead Developer
    in reply to Peter Lewis

    Additionally, in the latter case, a designer should consider the corner case where person A enters an Email or phone number that *actually* belongs to person B, and then (later) person B tries to enter it - should the system now not let them enter it, because a previous user fat-fingered and/or stole theirs?  As someone with a "first.last" gmail address, this issue is deeply personal to me...

  • 0
    Certified Associate Developer
    in reply to Peter Lewis

    Hi, thanks for replying.

    Yes! I am looking for the second, which means, email or mobile number that must be unique across the application. How can apply it to the interface... at the time of entering... it should show an error message. 

  • Every input field has a "validations" attribute. You can invoke a rule here, and in that rule you can use the value entered by the user to search the existing values in your database table. If you find a corresponding value you can return an appropriate message, else return null. 

  • 0
    Certified Senior Developer

    Hi Shubham, you can get all email ids from db in a local variable and then configure the validation parameter of the text field using an expression (use "contains" function) which will check if entered value exists or not in the local variable you pulled of email ids. And the. Conditionally you can show some message to the user.

  • I don't think fetching ALL emailids into a local variable and then conducting a local lookup is very scalable. Imagine if the database had 10 million unique values for email addresses? Better to run a Query Entity to match on the entered emaiId and restrict the result set to 1 row (batchSize: 1),. The resultant datasbuset would wither have 0 or 1 items in the returned data.

  • 0
    Certified Senior Developer
    in reply to Stewart Burchell

    You are right Stewart. For scalibility , your approach would be great. I just thought about less data in db.

  • Here's an example of how you could set it up for a phone number:

    a!localVariables(
      local!phoneNumber,
      
      /* Determines how many existing records */
      /* have the same phone number */
      local!phoneNumberMatching: if(
        a!isNullOrEmpty(local!phoneNumber),
        0,
        
        /* Run a query with a filter by phone number. */
        /* This could also use a!queryEntity() */
        a!queryRecordType(
          recordType: recordType!Employee
          fields: {
            recordType!Employee.fields.name,
            recordType!Employee.fields.phoneNumber
          },
          pagingInfo: a!pagingInfo(
            startIndex: 1,
            batchSize: 1
          ),
          filters: a!queryFilter(
            field: recordType!Employee.fields.phoneNumber
            operator: "=",
            value: local!phoneNumber
          )
        ).totalCount
      ),
      
      /* Checks that the there are no matches */
      local!phoneNumberIsValid: and(
        a!isNotNullOrEmpty(local!phoneNumber),
        local!phoneNumberMatching = 0
      ),
      a!formLayout(
        label: "Form",
        contents: {
          a!textField(
            label: "Text",
            labelPosition: "ABOVE",
            saveInto: local!phoneNumber,
            value: local!phoneNumber,
            refreshAfter: "UNFOCUS",
            validations: {
              if(
              local!phoneNumberIsValid,
                null,
                "This phone number has already been taken, please enter a different phone number"
              ),
            }
          )
        },
        buttons: a!buttonLayout(
          primaryButtons: {
            a!buttonWidget(
              label: "Submit",
              submit: true,
              style: "PRIMARY",
              loadingIndicator: true,
            )
          },
        )
      )
    )

    Keep in mind that validations for phone numbers can get fairly tricky - often different users will include dashes, parentheses, dots, etc to separate the values of the phone number, so you may either want additional validations to account for those cases or use some kind of cleaning to make sure that you're only comparing the numbers.

  • 0
    Certified Associate Developer
    in reply to Peter Lewis

    Thanks a lot. That's working fine. :) 

  • 0
    Certified Associate Developer
    in reply to GopalK

    Thanks Gopalk, but getting all email ids will not be right solution if data exceeded in future. performance will get slow.

    By the way, Thank you so much for your answer.