ValidateEmail 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ValidateEmail/version.rb +1 -1
- data/lib/ValidateEmail.rb +2 -1
- data/test/validate_email_test.rb +35 -18
- metadata +2 -2
data/lib/ValidateEmail.rb
CHANGED
@@ -2,7 +2,8 @@ require 'resolv'
|
|
2
2
|
|
3
3
|
module ValidateEmail
|
4
4
|
def self.validate(email, validate_mx = false)
|
5
|
-
|
5
|
+
email_pattern = (email =~ /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/)
|
6
|
+
is_valid = email_pattern.nil? ? false : true
|
6
7
|
is_valid = is_valid && validate_mx_record(email) if validate_mx
|
7
8
|
|
8
9
|
return is_valid
|
data/test/validate_email_test.rb
CHANGED
@@ -5,24 +5,41 @@ require 'ValidateEmail'
|
|
5
5
|
|
6
6
|
class ValidateEmailTest < Test::Unit::TestCase
|
7
7
|
def test_validat_email
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
8
|
+
assert_not_valid("bvsatyaram")
|
9
|
+
assert_not_valid("bvsatyaram@example")
|
10
|
+
assert_not_valid("bvsatyaram.com")
|
11
|
+
assert_valid("bvsatyaram@example.com")
|
12
|
+
assert_valid("bv_satyaram@example.com")
|
13
|
+
assert_valid("bv.satyaram@example.com")
|
14
|
+
assert_valid("bv-satyaram@example.com")
|
15
|
+
assert_valid("bvsatyaram@example.org")
|
16
|
+
assert_valid("bvsatyaram@fhgtrityhgs.com")
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
18
|
+
assert_not_valid("bvsatyaram", true)
|
19
|
+
assert_not_valid("bvsatyaram@example", true)
|
20
|
+
assert_not_valid("bvsatyaram.com", true)
|
21
|
+
assert_valid("bvsatyaram@bvsatyaram.com", true)
|
22
|
+
assert_valid("bv_satyaram@bvsatyaram.com", true)
|
23
|
+
assert_valid("bv.satyaram@bvsatyaram.com", true)
|
24
|
+
assert_valid("bv-satyaram@bvsatyaram.com", true)
|
25
|
+
assert_valid("bvsatyaram@rubygems.org", true)
|
26
|
+
assert_not_valid("bvsatyaram@fhgtrityhgs.com", true)
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def assert_valid(email, validate_mx = nil)
|
31
|
+
if validate_mx.nil?
|
32
|
+
assert_equal true, ValidateEmail.validate(email)
|
33
|
+
else
|
34
|
+
assert_equal true, ValidateEmail.validate(email, validate_mx)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def assert_not_valid(email, validate_mx = nil)
|
39
|
+
if validate_mx.nil?
|
40
|
+
assert_equal false, ValidateEmail.validate(email)
|
41
|
+
else
|
42
|
+
assert_equal false, ValidateEmail.validate(email, validate_mx)
|
43
|
+
end
|
27
44
|
end
|
28
45
|
end
|