The CreatorCon Call for Content is officially open! Get started here.

Validate email field input

gaidem
ServiceNow Employee
ServiceNow Employee

I have a client script which will ensure input into an email field contains a valid email address:



function onChange(control, oldValue, newValue, isLoading, isTemplate) {
//If the page isn't loading
if (!isLoading) {
//If the new value isn't blank
if(newValue != '') {
//Type appropriate comment here, and begin script below
var oldV = oldValue;
validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
strEmail = g_form.getValue(email);

// search email text for regular exp matches
if (strEmail.search(validRegExp) == -1)
{
alert('A valid e-mail address is required.');
g_form.setValue('email',oldV);
}


}
}
}

14 REPLIES 14

Sadly portal and mobile do not work with a get control ID so you can't use a drop in script like that in the portal.. HOWEVER if you functionalize the script and remove the control ID it SHOULD work perfectly.



Mobile Client GlideForm (g form) Scripting - ServiceNow Wiki



see section 2.1.5 and 2.1.6


I use this in the Service Portal, and it works perfectly.



function onChange(control, oldValue, newValue, isLoading) {


    if (isLoading || newValue == '') {


          return;


    }



  var email = newValue + '';


  var regEx = new RegExp(/^(([^<>()\[\]\\.,;:\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,}))$/);


  g_form.hideFieldMsg('u_personal_email');



  if (!regEx.test(email)) {


  g_form.setValue('u_personal_email', '');


  g_form.showFieldMsg('u_personal_email', 'Please enter a valid email address', 'error');


  }


}


I wrote about this approach in the SNStud.io blog: Validating Email Addresses in Service Portal


oharel
Kilo Sage

Did you try to just add an OOB email field to the form?


  1. Make the email field visible (need admin and elevated roles):
    1. akash.sharma@trianz.com wrote in the thread Email Field type validation   that you will need to activate admin override   in the write acl for "sys_glide_object".
    2. Make email field visible from Field class table.
  2. Add an field to your form.

Don't forget to de-activate admin override that you previously enabled.



harel


Email is only available as a type in Istanbul