0

I have a form that asks the user to enter a hex color value. We ask for this in many places (different values, about 20 times).

Is there a way to require the "#" to begin the entry?

Is there some way to test to validate that the entry only containing 0-9 and A-F?

1 Answer 1

2

I'm a developer with Cognito Forms.

Here's how to accomplish what you're describing:

  1. Create a Textbox field to collect a Hex value. In this example, our field is labelled Text.

  2. In the Textbox field settings, set the Default Value to the expression below. This expression will shift any letters inputted into uppercase.

=Text.ToUpper()

  1. At the bottom of the Textbox field settings, select the Show Custom Error - When option, and use the expression below. This expression ensures that any inputted value must begin with a pound sign, and must not contain any letter of the alphabet after F.

=(!Text.StartsWith("#") or Text.IndexOfAny(['G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']) > -1)

  1. Write the custom message that appears when an error occurs. It can be a simple static message like the following:

enter image description here

Or, you can create a conditional message using an if/then statement:

=if !Text.StartsWith("#") then "Value must begin with a #." else if Text.IndexOfAny(['G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'])> -1 then "Please use letters A-F." else ""

This will let users know which specific error is occurring.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.