summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2017-09-09 17:37:13 +0200
committerHarald Eilertsen <haraldei@anduin.net>2017-09-09 17:37:13 +0200
commit5de07c194e85fa74e7d7f2be12e99cb9acad3ccc (patch)
treefc58dc46aba60b898afec89a1cb5b451da07d236 /lib
parent032d46f65ac3bbfe5c05f96e6b68d0dfe2a4aa3d (diff)
downloadnorsk-urskog-registrations-5de07c194e85fa74e7d7f2be12e99cb9acad3ccc.tar.gz
norsk-urskog-registrations-5de07c194e85fa74e7d7f2be12e99cb9acad3ccc.tar.bz2
norsk-urskog-registrations-5de07c194e85fa74e7d7f2be12e99cb9acad3ccc.zip
Validate postcode properly.
Don't accept invalid postcodes, or postcodes containing invalid chars.
Diffstat (limited to 'lib')
-rw-r--r--lib/contact.rb27
1 files changed, 19 insertions, 8 deletions
diff --git a/lib/contact.rb b/lib/contact.rb
index ffa33bf..cb9a6e9 100644
--- a/lib/contact.rb
+++ b/lib/contact.rb
@@ -16,28 +16,39 @@ class Contact
"#{@street}, #{@postcode} #{@city}"
end
+ def valid?
+ validate!
+ @errors.length == 0
+ end
+
def validate!
- errors = []
+ @errors = []
if @name.nil? || @name.strip.empty?
- errors << "Du må oppgi en kontaktperson"
+ @errors << "Du må oppgi en kontaktperson"
end
- if @postcode.nil? || @postcode.strip.empty?
- errors << "Du må oppgi et gyldig postnr."
+ unless valid_postcode
+ @errors << "Du må oppgi et gyldig postnr."
end
if @city.nil? || @city.strip.empty?
- errors << "Du må oppgi poststed."
+ @errors << "Du må oppgi poststed."
end
if @phone.nil? || @phone.strip.empty?
- errors << "Du må oppgi et telefonnummer"
+ @errors << "Du må oppgi et telefonnummer"
end
if @email.nil? || @email.strip.empty?
- errors << "Du må oppgi en epostadresse"
+ @errors << "Du må oppgi en epostadresse"
end
- errors
+ @errors
+ end
+
+ private
+
+ def valid_postcode
+ @postcode && @postcode =~ /^\d{4}$/
end
end