Select to view content in your preferred language

Constraint or validation rule to check that text field matches a pattern?

141
3
Jump to solution
Tuesday
AlfredBaldenweck
MVP Regular Contributor

Basically, I'd like to enforce a text field to follow a pattern like [A-Z]{4}\s{4}\d+, to use the regex for it. (Four letters followed by 4 whitespace chars, followed by 1 or more digits

Has anyone had any luck doing this?

0 Kudos
2 Solutions

Accepted Solutions
Jake_S
by Esri Contributor
Esri Contributor

@AlfredBaldenweck 

Give this a go. I used this as the testing value 

DDDD    1212121212



/*
Arcade Constraint Rule: Checks if a field matches the pattern:
- Four letters (A-Z, case-insensitive)
- Four whitespace characters
- One or more digits

Usage: Replace $feature.Name with your actual field name.
*/

var value = $feature.Name;

// Check if value is empty
if (IsEmpty(value)) {
    return false;
}

// Check length: at least 9 (4 letters + 4 spaces + 1 digit)
if (Count(value) < 9) {
    return false;
}

// Check first 4 are letters
for (var i = 0; i < 4; i++) {
    var ch = Mid(value, i, 1);
    if (Find(Upper(ch), "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == -1) {
        return false;
    }
}

// Check next 4 are whitespace
for (var i = 4; i < 8; i++) {
    if (Mid(value, i, 1) != " ") {
        return false;
    }
}

// Check remaining are digits
for (var i = 8; i < Count(value); i++) {
    var ch = Mid(value, i, 1);
    if (Find(ch, "0123456789") == -1) {
        return false;
    }
}

return true;

View solution in original post

JayCary
Esri Contributor

Not an Arcade-based solution, but the Data Reviewer extension's Regular Expression check can be used to create validation attribute rules to evaluate string formatting.

View solution in original post

3 Replies
Jake_S
by Esri Contributor
Esri Contributor

@AlfredBaldenweck 

Give this a go. I used this as the testing value 

DDDD    1212121212



/*
Arcade Constraint Rule: Checks if a field matches the pattern:
- Four letters (A-Z, case-insensitive)
- Four whitespace characters
- One or more digits

Usage: Replace $feature.Name with your actual field name.
*/

var value = $feature.Name;

// Check if value is empty
if (IsEmpty(value)) {
    return false;
}

// Check length: at least 9 (4 letters + 4 spaces + 1 digit)
if (Count(value) < 9) {
    return false;
}

// Check first 4 are letters
for (var i = 0; i < 4; i++) {
    var ch = Mid(value, i, 1);
    if (Find(Upper(ch), "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == -1) {
        return false;
    }
}

// Check next 4 are whitespace
for (var i = 4; i < 8; i++) {
    if (Mid(value, i, 1) != " ") {
        return false;
    }
}

// Check remaining are digits
for (var i = 8; i < Count(value); i++) {
    var ch = Mid(value, i, 1);
    if (Find(ch, "0123456789") == -1) {
        return false;
    }
}

return true;

AlfredBaldenweck
MVP Regular Contributor

This worked pretty well, but is unfortunately more involved than I was hoping. 

@JayCary 's suggestion of using Data Reviewer also worked out ok, but has its own set of drawbacks.

Dropping this link to a relevant Idea Arcade: Regular Expression Support - Esri Community to add regex to Arcade.

Thanks!

0 Kudos
JayCary
Esri Contributor

Not an Arcade-based solution, but the Data Reviewer extension's Regular Expression check can be used to create validation attribute rules to evaluate string formatting.