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

sm00420
Tera Contributor

Hi


I am unable to find isEmailValid() function. Could you please tell us where we can find this function in API.



Thanks in Advance


hwest
Kilo Explorer

Has anyone tried the script provided by Matt and had it work? We have tried it in two different places in our QA environment and not had any luck. Any input is appreciated!


randrews
Tera Guru

I use the below as an onchange script for the field being validated.. one thing you may want to incorporate that i did was the var err_field=control.id;



this makes the code portable.. you just drop it into the onchange script and set the field you are watching and it will get the name of the field for the script by itself.



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


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


          return;


    }


              //validates a valid email address for the onchange field


              var err_field = control.id;


              var err_message = 'Invalid Email Address (abc@domain.com)',


              regex = /[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/ig;


              var err_flag = 'false';


              err_flag = regex.test(g_form.getValue(err_field));


              g_form.hideErrorBox(err_field);


              if (!err_flag) {


                      g_form.showFieldMsg(err_field, err_message, 'error');


              }


              else { g_form.hideErrorBox(err_field); }


}



for those that want it the below does phone numbers <us only>



On change script


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


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


          return;


    }



    //checks the variable the onchange is for to verify it is a valid phone number


          var phone_field = control.id;


          g_form.hideFieldMsg(phone_field);


          var intNum = g_form.getValue(phone_field);



          if (!isPhoneDash(intNum))


          {


g_form.showFieldMsg(phone_field,'Invalid Phone Number Value ###-###-####','error');


          }


   


}


apj_pga
Kilo Contributor

Raymond these are elegant and very useful. I'm new to admin and this is a huge help (took me forever to find but I'm so glad I found your script). Thank you!


Hey Raymond,



Would you know how to make that script work in Service Portal?



Thanks