Search code examples
clojureclojurescript

how to validate email in clojure


I want to validate a email using clojure. How can I do it.

(defn validate-email [email]

    )

I want to know how will be the function body writeen using regular expressions.


Solution

  • (defn validate-email
      [email]
      (let [pattern #"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"]
        (and (string? email) (re-matches pattern email))))
    

    As far as regular expressions in Clojure, this might help: http://www.lispcast.com/clojure-regex


    Or, funcool/struct can be used to validate email, beside other things.