2012-12-17 17 views
4

添加簡單的電子郵件驗證我的代碼,我創建了以下功能:驗證電子郵件的一行在斯卡拉

def isValid(email: String): Boolean = if("""(?=[^\s]+)(?=(\w+)@([\w\.]+))""".r.findFirstIn(email) == None)false else true 

這將通過電子郵件像[email protected]和失敗一樣bobtestmymail.com郵件,但隨着空格字符的郵件滑過,如bob @testmymail也將返回true。

我可能是傻在這裏...

+1

驗證電子郵件的正確正則表達式將是巨大的。例如,請參閱[這裏](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address)。 –

+0

感謝安德魯,我意識到這一點,這就是爲什麼我只是在這裏添加一個'簡單'的規則;-)它是否有一個'@'它有''。並且它沒有空間。這只是一個簡單的檢查來測試打字錯誤,而不是滿足所有情況。重新寫它是根據我的待辦事項:-p – Jack

+2

也不是一個答案 - 儘管我對這個問題有點不清楚,無論如何 - 但滾動自己的電子郵件驗證可能不是一個好主意。有可能你已經在使用一個提供一個的框架,如果沒有的話,[Apache Commons可以](http://commons.apache.org/validator/apidocs/org/apache/commons/validator/routines/EmailValidator.html )。 –

回答

4

正如我已經測試你的正則表達式,它正趕上簡單的電子郵件,我再檢查你的代碼,看到你使用findFirstIn。我相信這是你的問題。 findFirstIn將跳轉所有空格,直到它匹配字符串中任何位置的某個序列。我相信,在你的情況下,最好使用unapplySeq並檢查是否返回部分列表

def isValid(email: String): Boolean = 
    if("""(?=[^\s]+)(?=(\w+)@([\w\.]+))""".r.findFirstIn(email) == None)false else true 

def isValid2(email: String): Boolean = 
    """(\w+)@([\w\.]+)""".r.unapplySeq(email).isDefined 

isValid("[email protected]")      //> res0: Boolean = true 

isValid("t es [email protected]")      //> res1: Boolean = true 

isValid("b ob @tes tmai l.com")     //> res2: Boolean = false 

isValid2("[email protected]")      //> res3: Boolean = true 

isValid2("t es [email protected]")      //> res4: Boolean = false 

isValid2("b ob @tes tmai l.com")     //> res5: Boolean = false 

// but those don't work for both: 
// I recommend you using a proper regex pattern to match emails 
isValid("[email protected]")     //> res6: Boolean = true 

isValid("[email protected]")       //> res7: Boolean = true 

isValid2("[email protected]")     //> res8: Boolean = true 

isValid2("[email protected]")      //> res9: Boolean = true 
+1

這個解決方案在所有情況下都不是真的有效:isValid2(」user @ provider「)會導致TRUE – sonix

1
scala> def isValid(email : String): Boolean = if("""^[-a-z0-9!#$%&'*+/=?^_`{|}~]+(\.[-a-z0-9!#$%&'*+/=?^_`{|}~]+)*@([a-z0-9]([-a-z0-9]{0,61}[a-z0-9])?\.)*(aero|arpa|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|[a-z][a-z])$""".r.findFirstIn(email) == None)false else true 
v: (email: String)Boolean 

scala> isValid("""[email protected]""") 
res0: Boolean = true 

scala> isValid("""bob @test.com""") 
res1: Boolean = false 

scala> isValid("""bobtest.com""") 
res2: Boolean = false 
+0

「」「\ b [a-zA-Z0-9。!# ?(?:\ [A-ZA-Z0-9 - ] +)| $%&'* +/=^_'{}〜 - - ] + @ [A-ZA-Z0-9] + * \ b「」「r這個不是那麼嚴格 – wleao

+0

謝謝你們,這對我有很大幫助 – Jack

14

我的函數是從一個播放框架使用(見PlayFramework)的啓發,並使用這裏介紹的正則表達式:W3C recommendation。希望能幫助到你。其他問題中建議的所有測試都已通過。

private val emailRegex = """^[a-zA-Z0-9\.!#$%&'*+/=?^_`{|}~-][email protected][a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$""".r 


def check(e: String): Boolean = e match{ 
    case null           => false 
    case e if e.trim.isEmpty       => false 
    case e if emailRegex.findFirstMatchIn(e).isDefined => true 
    case _            => false 
}