2016-03-16 66 views
3

我目前使用此代碼爲我的電子郵件驗證:Rails的正則表達式驗證電子郵件允許

/\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 

的問題是,它仍然接受與喜歡ab [email protected]空間的電子郵件。

我已經嘗試了很多代碼的變化,不應該允許的空間,但而不是用在表單頂部的錯誤消息,刷新頁面,它給了我這個錯誤:

The provided regular expression is using multiline anchors (^ or $), which may present a security risk. Did you mean to use \A and \z, or forgot to add the :multiline => true option?

+0

缺少錨'^'和'$'。 – Tushar

+0

試試這個^ [_A-Za-z0-9 - \\ +] +(\\。[_A-Za-z0-9 - ] +)* @ [A-Za-z0-9 - ] + 。\ [A-ZA-Z0-9] +)*(\\ [A-ZA-Z] {2,})$; –

+0

你需要你的用戶來驗證他們的電子郵件地址嗎? –

回答

0

檢查:

/^[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}$/i 
+1

這並不回答問題,因爲問題中列出的正則表達式已拒絕'ab [email protected]'。它也是過度限制性的,並且將禁止來自非拉丁字母域和一些通用頂級域的電子郵件地址。 –

+0

@Lucas我剛剛檢查出來。它的輸入和其他輸入也很好。在rubular.com檢查。你可以請重新檢查它 – Ranzit

+1

我不是說這不是一個有效的正則表達式,我說它不會回答原來的問題(因爲你的和原始問題都拒絕'ab [email protected]'),並且它可以使關於領域部分的假設變得更糟。另見http://stackoverflow.com/a/25982750。例如,''[email protected]'=〜/^[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[AZ]{2,4}$/i => nil' ...但這是一個有效的電子郵件地址。 –

2

你缺少的開始和結束錨標記,以便您可以參考以下鏈接Regular expressions with validations in RoR 4和您的正確的正則表達式會,

/^[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}$/i 

你應該嘗試這個正則表達式也用於電子郵件驗證,這個正則表達式將滿足你的所有要求,並檢查所有可能的驗證。

^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$ 

,這裏是正則表達式的解釋,這將是有益的理解驗證

^   #start of the line 
    [_A-Za-z0-9-\\+]+ # must start with string in the bracket [ ], must contains one or more (+) 
    (  # start of group #1 
    \\.[_A-Za-z0-9-]+ #  follow by a dot "." and string in the bracket [ ], must contains one or more (+) 
)*   # end of group #1, this group is optional (*) 
    @   #  must contains a "@" symbol 
    [A-Za-z0-9-]+  #  follow by string in the bracket [ ], must contains one or more (+) 
     (  #   start of group #2 - first level TLD checking 
     \\.[A-Za-z0-9]+ #   follow by a dot "." and string in the bracket [ ], must contains one or more (+) 
    )*  #   end of group #2, this group is optional (*) 
     (  #   start of group #3 - second level TLD checking 
     \\.[A-Za-z]{2,} #   follow by a dot "." and string in the bracket [ ], with minimum length of 2 
    )   #   end of group #3 
$   #end of the line 

測試輸出:

Email is valid : [email protected] , true 
Email is valid : [email protected] , true 
Email is valid : [email protected] , true 
Email is valid : [email protected] , true 
Email is valid : [email protected] , true 
Email is valid : [email protected] , true 
Email is valid : [email protected] , true 
Email is valid : [email protected] , true 
Email is valid : [email protected] , true 
Email is valid : [email protected] , true 
Email is valid : abc , false 
Email is valid : [email protected] , false 
Email is valid : [email protected] , false 
Email is valid : [email protected] , false 
Email is valid : [email protected] , false 
Email is valid : [email protected] , false 
Email is valid : abc()*@gmail.com , false 
Email is valid : [email protected]%*.com , false 
Email is valid : [email protected] , false 
Email is valid : [email protected] , false 
Email is valid : [email protected]@gmail.com , false 
Email is valid : ab [email protected] , false 
Email is valid : [email protected] , false 
+0

問題中的正則表達式不是問題,它已經拒絕'ab [email protected]'。將正則表達式更改爲其他內容不會解決問題中概述的行爲。此外,此正則表達式將拒絕有效的非拉丁字母域名和一些有效的gTLD域名。 –

+0

@LucasNelson,我檢查了正則表達式也只有開始和結束錨缺失,所以他可以參考這個鏈接http://stackoverflow.com/questions/17759735/regular-expressions-with-validations-in-ror-4和在這裏我正在更多的電子郵件驗證案例。 –

0

嘗試了用不同的字符串正則表達式表明, _does_not_接受看起來像空格郵件的東西

'[email protected]' =~ /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    => 0 

'a [email protected]' =~ /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    => nil 

'abc [email protected]' =~ /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    => nil 

'[email protected] gle.com' =~ /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    => nil 

但是正則表達式存在一些問題。 這些字符串不應該是有效的匹配,但它們分別是:

'[email protected]' =~ /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    => 0 

'[email protected]' =~ /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    => 0 

這個正則表達式避免了這個問題:(請注意,需要轉義!)

/\A([\w+\-]+\.)+[\w+\-][email protected]([a-z\d\-]+\.)+[a-z]+\z/i 

,但它仍然是不夠一般。

你可能要檢查這個問題,以改善正則表達式:

What characters are allowed in an email address?

然而,這裏被認爲有可能要用考慮:

不要驗證電子郵件正則表達式,而是通過要求用戶點擊您發送給他的確認電子郵件中的鏈接來隱式驗證

要實施確認電子郵件,您可以使用Devise confirmmable或手動構建:https://www.youtube.com/watch?v=EycP9L_x5rE

確認電子郵件比試圖找到世界上所有電子郵件地址的完美正則表達式要容易得多,也更直接。

0

您可以使用URI::MailTo::EMAIL_REGEXP

相關問題