Field Validation with Pattern

Good Afternoon,

I have a form that I have added a Common Field Text Box and have set the pattern type to Custom and added in a custom Pattern to enforce users to input a UK Postcode in the correct format, the regexp pattern is: -

^$|([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9][A-Za-z]?))))\s?[0-9][A-Za-z]{2})$

The issue I have is that adding an address to the form is optional so the field is only required when a user chooses to add an address, the regexp ^$|above should also allow an empty string to validate. However if the field is left empty I still receive the custom error message This field must contain a valid UK post code.

Firstly why is the else empty string not working?

Secondly as a workaround could I use Javascript to set or clear the pattern as required when the user clicks the button to add an address to the form.

Thanks

Dear @Tony_Duke,

Please try to add this code:

fd.rendered(function() {
    if (fd.field("TextBox0").value == null) {
        fd.field("TextBox0").value = "";
    }
})  

Notice that you should replace “TextBox0” with the internal name of your field.