Check email/ phone number formatting

Hi,

  1. Is it possible to check if phone numbers field have 10 digits and if not display warning
  2. Is there a way auto format a phone number input to: xxx-xxx-xx
  3. Check if email address is in an xxxx@xxxx.xxx format and if not display warning

Thanks,

Dominic

Hi @dominic

Thank you for your questions.

Telephone number

  1. Is it possible to check if phone numbers field have 10 digits and if not display warning
  2. Is there a way auto format a phone number input to: xxx-xxx-xx

Yes, both things are possible.
Here is code example:


{% comment %}Checking if telephone number contains 10 digits{% endcomment %}
{% if custom.telephone.number != blank and telephone_number.size != 10 %}
  {% ic %}{::warningtext}Telephone number should contain 10 digits{:/warningtext}{% endic %}
{% endif %}

{% comment %}Telephone number format to display{% endcomment %}
{% capture telephone_number_to_display %}{% assign phone = telephone_number | split:"" %}{{ phone[0] }}{{ phone[1] }}{{ phone[2] }}-{{ phone[3] }}{{ phone[4] }}{{ phone[5] }}-{{ phone[6] }}{{ phone[7] }}{{ phone[8] }}{{ phone[9] }}{% endcapture %}

{% comment %}Telephone number input / preview mode{% endcomment %}
{% ic %}{% input custom.telephone.number placeholder:"Phone number" %}{% endic %}
{% nic %}{{ telephone_number_to_display }}{% endnic %}

E-mail address

  1. Check if email address is in an xxxx@xxxx.xxx format and if not display warning

This is also possible.
Code example:

{% capture email_address %}{{ custom.email.address }}{% endcapture %}

{% comment %}Checking if e-mail address contains symbols "@" and "." {% endcomment %}
{% stripnewlines %}
{% if custom.email.address != blank %}
  {% unless email_address contains '@' %}
    {% comment %}e-mail addrress does not contains "@"{% endcomment %}
    {% ic %}{::warningtext}E-mail address should contain @ symbol{:/warningtext}{% newline %}{% endic %}
      {% else %}{% comment %}e-mail addrress contains "@"{% endcomment %}
        
        {% assign email_address_part = email_address | split:"@" %}
        
        {% comment %}Checking if first part of e-mail address contains "."{% endcomment %}
        {% assign email_address_part1 = email_address_part[0] %}
          {% if email_address_part1 contains "." %}
            {% ic %}{::warningtext}Beginning of e-mail address {{ email_address_part1 }} should not contain "." {:/warningtext}{% newline %}{% endic %}
          {% endif %}
        
        {% comment %}Checking if first part of e-mail address contains "."{% endcomment %}
        {% assign email_address_part2 = email_address_part[1] %}
          {% unless email_address_part2 contains "." %}
            {% ic %}{::warningtext}End of e-mail address {{ email_address_part1 }} should contain "." {:/warningtext}{% newline %}{% endic %}
          {% endunless %}
          
  {% endunless %}
{% endif %}
{% endstripnewlines %}

{% input custom.email.address placeholder:"E-mail address" assign:email_address %}

Please let us know if you need any additional information.

Jelena

1 Like

Thanks Jelena!