Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Email Validation for single line text field (Email ID)

GAyathri33
Tera Contributor

Hi Team,

 

How to write catalog client script to give email validation as it can accept only @(companyname).com users only.

 

Field is a single line text field(email Id).

 

Thanks  

1 ACCEPTED SOLUTION

@GAyathri 

Not sure but that option should be there for single line text variable

If not then try to set it from List view of the variable if you don't wish to add to form layout

find_real_file.png

OR

you can create onChange client script on that variable and sample script below

UI Type - ALL

Script:

 function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading) {
		return;
	}

	g_form.hideErrorBox('variable');

	var regex = /^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@companyname.com$/;

	newValue = newValue.toString();
	var isValidFormat = regex.test(newValue);

	if(!isValidFormat){
		g_form.showErrorBox('variable','Please enter valid email address','error');
		g_form.clearValue('variable');
	}

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

21 REPLIES 21

rwandamc
Kilo Explorer

The fully RFC 822 compliant regex is inefficient and obscure for validate email address because of its length. Fortunately, RFC 822 was superseded twice and the current specification for email addresses is RFC 5322. RFC 5322 leads to a regex that can be understood if studied for a few minutes and is efficient enough for actual use.

If you use HTML5, use this code:

<input type="email" name="email" required placeholder="Enter a valid email address">

 

Ian Mildon
Tera Guru

There is a ServiceNow created onSubmit Client Script that comes with the "Human Resources: Core" application called Validate Email onSubmit.

Here is the 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;
	}