cancel
Showing results for 
Search instead for 
Did you mean: 

How to validate a email address from groovy scripting

SOLVED
Highlighted
Contributor

How to validate a email address from groovy scripting

import net.sf.json.groovy.*;
import net.sf.json.groovy.JsonSlurper;

def Res = context.expand( '${Users - Request 1#Response}' )

def slurper = new JsonSlurper()

def json = slurper.parseText Res
def Email = json.email

From the above code I got an array of emails from my response data set.

Now i want to check that all the emails has a "@" sign. and at least one "." .

How can i do this.

 

For now at least "@" verification is enough 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Contributor

Re: How to validate a email address from groovy scripting

I did by below code.

 

import net.sf.json.groovy.*;
import net.sf.json.groovy.JsonSlurper;

def Res = context.expand( '${Users - Request 1#Response}' )

def slurper = new JsonSlurper()

def json = slurper.parseText Res
def Email = json.email

int i =0;

Email.each()
{
	assert Email[i].contains('@')
	
	log.info Email[i]
	i++
}

View solution in original post

2 REPLIES 2
Highlighted
Contributor

Re: How to validate a email address from groovy scripting

I did by below code.

 

import net.sf.json.groovy.*;
import net.sf.json.groovy.JsonSlurper;

def Res = context.expand( '${Users - Request 1#Response}' )

def slurper = new JsonSlurper()

def json = slurper.parseText Res
def Email = json.email

int i =0;

Email.each()
{
	assert Email[i].contains('@')
	
	log.info Email[i]
	i++
}

View solution in original post

Highlighted
Community Hero

Re: How to validate a email address from groovy scripting

Hey,

 

I would've thought you can use jsonpath regex match assertion if you want to use the OTB functionality - you just need to determine the regex for your email address - email addresses are a littel awkward with regex - there's lots of different options trying to cover all permutations.

 

Found via link

 

Here's one --> ^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$

 

And this covers the following:  Doesn't allow numbers in the domain name and doesn't allow for top level domains that are less than 2 or more than 3 letters (which is fine until they allow more). Doesn't handle multiple "." in the domain (joe@abc.co.uk).  If the 'allowed' email addresses are different - tailor the above regex accordingly

 

If you just need literally something to verify that the @ is in the email address and that the email address is comprised of up to 255 chars you could try the following regex (which I've just put together now) [a-z][A-Z][0-9]{250}@.[a-z][A-Z]{3} - this is a lot more basic than the above regex from the link - but it might do the job.

 

I suppose as you're using a script, you might want to add the assertion into your script - with this in mind, please see the following stackoverflow link which has details on asserting regex values

 

I'm not a scripter - so I can't give you the code - but hopefully the details/links above might help get you there

 

hope the above helps,

 

richie

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
New Here?
Join us and watch the welcome video:
Announcements
TechCorner Leaderboard
Compete with community members in the TechCorner Challenge and get into the Leaderboard!

Rank Participant Points Earned
1 msiadak 12
2 nmrao 8
3 HimanshuTayal 3
Top Kudoed Authors