From c6a94277ed64511833005920a77e62b3029ab4bf Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 2 Oct 2017 13:16:08 +0200 Subject: Validate contact phone and email. --- lib/contact.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/contact.rb b/lib/contact.rb index cb9a6e9..d5fd267 100644 --- a/lib/contact.rb +++ b/lib/contact.rb @@ -35,12 +35,12 @@ class Contact @errors << "Du må oppgi poststed." end - if @phone.nil? || @phone.strip.empty? - @errors << "Du må oppgi et telefonnummer" + unless valid_phone? + @errors << "Telefonnummer mangler eller er ugyldig" end - if @email.nil? || @email.strip.empty? - @errors << "Du må oppgi en epostadresse" + unless valid_email? + @errors << "E-postadresse mangler eller er ugyldig" end @errors @@ -51,4 +51,12 @@ class Contact def valid_postcode @postcode && @postcode =~ /^\d{4}$/ end + + def valid_email? + @email && @email =~ /^[A-Z0-9._%+-]+@(?:[A-Z0-9-]{1,63}\.)+[A-Z]{2,}$/i + end + + def valid_phone? + @phone && @phone =~ /^\+*[0-9- ]{8,}$/ + end end -- cgit v1.2.3