Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate email one-liner in Scala

Tags:

regex

scala

Adding simple e-mail validation to my code, I created the following function:

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

This will pass emails like [email protected] and fail mails like bobtestmymail.com, but mails with space characters slip through, like bob @testmymail will also return true.

I'm probably being silly here...

like image 762
Jack Avatar asked Dec 17 '12 10:12

Jack


People also ask

How do I validate an email address in regex?

To get a valid email id we use a regular expression /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/. According to http://tools.ietf.org/html/rfc3696#page-5 !

Can email be validated with regex?

Regex provides the ability to validate the structure of an email address. It can be handled with one or two lines of code and can easily be tweaked to handle a wide variation of different parameters.

How do I write an email validation pattern?

<input type="email" name="email" pattern="[a-z0-9. _%+-]+@[a-z0-9. -]+\. [a-z]{2,}$" title="please enter valid email [[email protected]]."> you can add title also to show a validity message.

Which are the two different ways to validate email addresses?

Most email service providers (ESPs) provide email validation services. There are many free tools that also validate email addresses; ValidateEmailAddress, EmailValidator and Pabbly Email Verification are few of such examples.


2 Answers

My function is inspired from the one that the Play Framework uses (see PlayFramework) and uses the regexp presented here: W3C recommendation. Hope it helps. All tests suggested in the other questions are passed.

private val emailRegex = """^[a-zA-Z0-9\.!#$%&'*+/=?^_`{|}~-]+@[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
}
like image 70
John Avatar answered Oct 13 '22 21:10

John


As I've tested your regex and it was catching simple emails, I then checked your code and saw that you're using findFirstIn. I believe that is your problem. findFirstIn will jump all the spaces until it matches some sequence anywhere in the string. I believe that in your case it's better to use unapplySeq and check if it returns Some List

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 t@gmailcom")                       //> res1: Boolean = true

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

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

isValid2("t es t@gmailcom")                      //> 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("test@gmailcom")                         //> res7: Boolean = true

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

isValid2("test@gmailcom")                        //> res9: Boolean = true
like image 4
wleao Avatar answered Oct 13 '22 19:10

wleao