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

Just tagged with new question pls check

Mohit Gupta5
Giga Contributor

Hi,

I have created one 'single line text' variable in catalog in catalog item and one catalog client script.

I have applied onChange client script in email field, find below code for catalog client script.

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

//Type appropriate comment here, and begin script below
var email = newValue;
var emailPattern = /^[a-zA-Z0-9._-]+@companyname.com$/;
if (!emailPattern.test(email)) {
alert('Please enter valid email.');
} else {
alert('Entered email is correct.');
}
}

 

 

Thanks,

Mohit

Saurabh Gaikwad
Mega Expert

Hello Gayathri,

use this line

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,}))*@companyname.com$/

 

 

I hope this will help you.

Mark Correct if this solves your issue and also mark ???? Helpful if applicable.

Regards,

Saurabh,

priyanka garad
Giga Expert

Hi,

Try below code ,

function onSubmit() {                      

var checkEmail=  g_form.getValue('u_emailid');

 var pattern=/^[a-zA-Z0-9]+@[a-z]{11}+\.[a-zA-Z]{2,4}$/;           

if(pattern.test(checkEmail)){                           

                                    alert("valid email id");

                                            }

                                      else{

                                         alert("Invalid email id")

                                              }

}

 

Please mark reply as Helpful/Correct, if applicable.

Thanks!!!!

Rishi Kendale
Giga Guru

Hello Gayathri,

Ankur Bawiskar has already provided the correct solution to your query. I would just explore it more. It depends on the business requirement.  You can directly use the inbuilt option of Regex provided with service catalog. But if your company has some specific requirements then you would need to go for scripting.
 

Eg.

If Email ID requirement is like below:

/*
1. max length = 30
2. before @ 4 to 16
3. after @ and before . 3 to 10
4. ending with .com or .co.in
5. first letter small case alphabet
*/

The single line email validation using Regex is as follows :

var regex = [a-z][a-zA-Z0-9.-]{3,15}@[a-zA-Z0-9]{3,10}.(com|co.in)

 Use this condition  in your scripting.

I hope this will be helpful for you. If it is helpful, please mark the answer as Helpful.

 

Warm regards,

Rishikesh Kendale