Is there a way to validate an email address

Cupcake
Mega Guru

I want to be able to validate if an email address is in the correct format.

Validate this is valid email address xxxxx@xxx.xxx - if not valid email, pop "Please enter valid email address"

Is there a way to do that?

Thanks,

Karen

1 ACCEPTED SOLUTION

Ian Mildon
Tera Guru

There is an onSubmit client script included OOB with HR Service Management that has the following validation script:

function onSubmit() {
   //Type appropriate comment here, and begin script below
   g_form.hideErrorBox('email');
   return validateEmail('email');
}

function validateEmail(field) { 
		var email = g_form.getValue(field);
		if (!email || email.trim().length <= 0)
			return true;
		var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
		if (!re.test(email)) {
			g_form.showErrorBox(field, getMessage('Invalid Email'));
			return false;
		}
		return true;
	}

You should be able to reuse this in a new client script for your issue.

View solution in original post

11 REPLIES 11

dvp
Mega Sage
Mega Sage

You can use the email type field or variable which will automatically validate the email address

Thank you for replying. I just made the variable of type Email; however, I was still able to submit the form.

So I am confused as to where it is validating the email address. And I don't want the form to be submitted if the email address is not of a valid format.

Thank you,

Karen

I just tested in my instance and it is stopping from submission in both backend view and service portal.

May i know which version you are using?

 

You can try with the below onChange client script

var valid = validateEmail(newValue);
if (!valid)
g_form.showErrorBox('recipient_email', getMessage('Invalid email address'));
else
g_form.hideErrorBox('recipient_email');

function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}

Ian Mildon
Tera Guru

There is an onSubmit client script included OOB with HR Service Management that has the following validation script:

function onSubmit() {
   //Type appropriate comment here, and begin script below
   g_form.hideErrorBox('email');
   return validateEmail('email');
}

function validateEmail(field) { 
		var email = g_form.getValue(field);
		if (!email || email.trim().length <= 0)
			return true;
		var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
		if (!re.test(email)) {
			g_form.showErrorBox(field, getMessage('Invalid Email'));
			return false;
		}
		return true;
	}

You should be able to reuse this in a new client script for your issue.