How to allow only email type in string field and restrict other string types?

Community Alums
Not applicable

HI,

   we have created a string field where user should enter the text in email format and it should not allow any other text type. please provide any solution.

Thanks,

Sirisha. 

4 REPLIES 4

Vismit Ambre
Giga Guru

Hi Sirisha,

You will need to write an onchange client script on your field or variable which would be something like this:

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

    var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    if (!newValue.match(mailformat)) {
        alert('You have entered an invalid email address!');
    }

}

Please mark my answer helpful/correct if this works for you.

 

Regards,

Vismit

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can use RegEx for this

Regards
Ankur

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

Imran Ahmad1
Kilo Guru

Hi @sirisha navuduri ,

 

Try this code I have tested code in my PDI.

Change the field name as per your requirement.

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

    var Reformat = /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/;
    if (!Reformat.test(newValue)) {
		g_form.clearValue("description");
        g_form.showFieldMsg("description",'Please enter valid email address!',"error");
    }
	
	

}

Mark as Correct answer and Helpful if applicable.

 

Reagrds,

Imran Ahmad.

The SN Nerd
Giga Sage
Giga Sage

If this is for Service Catalog, you can use OOB email validation.

See https://docs.servicenow.com/bundle/sandiego-servicenow-platform/page/product/service-catalog-managem...

OOB Email validation rule can be seen below:

/nav_to.do?uri=question_regex.do?sys_id=e005e481530020107d13ddeeff7b12e5


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022