0

How can I check that a string is valid email address in twig template ?

For example, in this code: {% set usernameType = (#Check user.username is email#) ? 'email' : 'number' %}

I need if user.username is a valid email address, usernameType set as email, otherwise set as number

Mohsen Zia
  • 442
  • 4
  • 16

1 Answers1

1

Twig is executed at the server side so if you want to validate email you should use javascript at client side like: https://stackoverflow.com/a/46181/4450882 in the server side you should use php or python to make this verification. php validation: https://www.php.net/manual/fr/filter.examples.validation.php python validation: https://pypi.org/project/validate_email/

surfman
  • 82
  • 3
  • Thanks for your reply. my view files extension is ".twig.html", so I cant use php codes! How can I call javascript function in twig tags ? – Mohsen Zia Jun 02 '20 at 10:23
  • you can use the block {% block javascripts %} {% endblock %} – surfman Jun 02 '20 at 10:30