Validate Email field

anupchandran
Kilo Contributor

Need a validation scripts to validate Email field. Field should validate (@ and .)

if somebody attempts to enter the email address without @ and .

1 ACCEPTED SOLUTION

Mike Allen
Mega Sage

There is a built in method:



if(oldValue != newValue){


  g_form.hideFieldMsg('email', true);


  if (!isEmailValid(newValue)) {


            g_form.showFieldMsg('email', newValue + " is an invalid email, please re-enter email in correct format.",'error');


  }


}


Capture.PNG


View solution in original post

10 REPLIES 10

If we want to validate that field on list view, How can we do that?

 

Bob
Giga Contributor

Thanks for this Mike! Also, just so people don't stall out like I did.


g_form.showFieldMsg('email', newValue + " is an invalid email, please re-enter email in correct format.",'error');


The 'email' portion needs to be the FIELD_NAME that you are showing the error.



Good stuff!


expert2016
Mega Contributor

Hi Anup,



Please use the below code!



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


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


          return;


    }




    //Validate email address


  var str = newValue;


  var n = str.indexOf("@");


  var n1 = str.indexOf(".");


  if (n == -1 || n1 == -1) {


alert('Please enter the valid email address!');


}


}


this is the simple script to validate the value of your email address to see   '@' and Period('.') strings.


Thanks jee


I was able to locate the actual code behind the ServiceNow isEmailValid() function.   I detailed it here:   https://community.servicenow.com/thread/237846#1006764