diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2017-10-02 13:16:08 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2017-10-02 13:16:08 +0200 |
commit | c6a94277ed64511833005920a77e62b3029ab4bf (patch) | |
tree | 48ef6e20554ce8ce01ced7380e5a01d57ec6c84f | |
parent | 580e8d292c0023e9c3391989c79f836c1cdcef9f (diff) | |
download | norsk-urskog-registrations-c6a94277ed64511833005920a77e62b3029ab4bf.tar.gz norsk-urskog-registrations-c6a94277ed64511833005920a77e62b3029ab4bf.tar.bz2 norsk-urskog-registrations-c6a94277ed64511833005920a77e62b3029ab4bf.zip |
Validate contact phone and email.
-rw-r--r-- | lib/contact.rb | 16 | ||||
-rw-r--r-- | spec/registration_spec.rb | 20 |
2 files changed, 32 insertions, 4 deletions
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 diff --git a/spec/registration_spec.rb b/spec/registration_spec.rb index 9dc7616..3ccfbe7 100644 --- a/spec/registration_spec.rb +++ b/spec/registration_spec.rb @@ -167,6 +167,26 @@ describe RegistrationApp do context 'with no contact email' do include_examples('form with errors', {:contact_email => ''}) end + + context 'with invalid contact email' do + context 'missing domain info' do + include_examples('form with errors', {:contact_email => 'blaff'}) + end + + context 'missing top level domain' do + include_examples('form with errors', {:contact_email => 'blaff@merz'}) + end + end + + context 'with invalid phone number' do + context 'containing invalid chars' do + include_examples('form with errors', {:contact_phone => '756balle'}) + end + + context 'containing too few numbers' do + include_examples('form with errors', {:contact_phone => '7568923'}) + end + end end context 'when registration is closed' do |