AEM – Custom Validation for Text Field

To add validation to a text field in an AEM dialog, you can use the “validation” attribute. The “validation” attribute allows you to specify the type of validation you want to perform on the text field.

Here are some examples of how to use the “validation” attribute to validate a text field:

  1. Required field validation:
<field
    jcr:primaryType="nt:unstructured"
    name="./myTextField"
    fieldLabel="My Text Field"
    xtype="textfield"
    allowBlank="false"
    required="true"/>

In this example, we set the “allowBlank” attribute to “false” and the “required” attribute to “true”. This ensures that the field cannot be left blank and must have a value.

  1. Minimum length validation:
<field
    jcr:primaryType="nt:unstructured"
    name="./myTextField"
    fieldLabel="My Text Field"
    xtype="textfield"
    minLength="5"
    vtype="alpha"/>

In this example, we set the “minLength” attribute to “5”. This ensures that the field must have at least 5 characters. We also set the “vtype” attribute to “alpha”. This ensures that the field only accepts alphabetical characters.

  1. Regular expression validation:
<field
    jcr:primaryType="nt:unstructured"
    name="./myTextField"
    fieldLabel="My Text Field"
    xtype="textfield"
    regex="^[a-zA-Z0-9_]*$"
    regexText="Only alphanumeric characters and underscores are allowed"/>

In this example, we set the “regex” attribute to a regular expression that allows only alphanumeric characters and underscores. We also set the “regexText” attribute to a custom error message that will be displayed if the field value does not match the regular expression.

These are just a few examples of how to use the “validation” attribute to validate a text field in an AEM dialog. You can customize the validation to meet your specific needs by using different validation attributes and values.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial