Best Way to Validate Email in a Field in a Record Producer?

Bob
Giga Contributor

I am currently looking at a Record Producer I created that needs to have many different fields that are inputting different emails. There are many articles that address the issue of email validation within a field, but I wanted to not have to modify each script individually if something needed changing. I came up with a number of options.

  • Create Catalog Client Scripts for onLoad/onChange
    • Too many fields to update and maintain. Makes me feel like less for not having a central function to rule them all.
  • Use oob Service Now email validation
    • Using the actual Email field Type, which does check the field automagically.
      • This field type is hidden? Why?
      • Should I override and un-hide email fields type?
        • Reasons against or for?
  • Another is using the built in function discussed in this article Validate Email field
    • Can't get the code to work below in my Catalog Client Script:
      1. function onChange(control, oldValue, newValue, isLoading) {
      2. if (isLoading || newValue == '') {
      3.           return;
      4.     }
      5.     //Email Validation Using OOB
      6. if(oldValue != newValue){  
      7.   g_form.hideFieldMsg('email', true);  
      8.   if (!isEmailValid(newValue)) {  
      9.             g_form.showFieldMsg('email', newValue + " is an invalid email, please re-enter email in correct format.",'error');  
      10.   }  
      11. }  


    • Am I missing something on how to properly call this function?
  • Create my own global Include/function
      • Haven't dug into this one yet!

Thanks in Advance!

Jason M Fisher

University of Kentucky

ITSM Developer

1 ACCEPTED SOLUTION

xiaix
Tera Guru

Well, I found some interesting stuff...



find_real_file.png



On line 9527 thru 9567:



find_real_file.png



Here's the actual code:


function isEmailValid(value) {


      var problemMsg = isEmailValidWithReason(value);


      if (problemMsg != "") {


              jslog("isEmailValid: " + problemMsg);


              return false;


      }


      return true;


}




function isEmailValidWithReason(value) {


      var localPartChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%*/?|^{}`~&'+-=_.";


      var domainChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";


      if (value.indexOf("@") == -1)


              return "missing @ sign";


      var s = value.split("@");


      if (s.length != 2)


              return "too many at signs";


      if (!containsOnlyChars(localPartChars, s[0]))


              return "invalid character before the at sign";


      if (s[0].length < 1)


              return "at least one character must be before the at sign";


      if (s[0].substr(0, 1) == ".")


              return "period cannot be the first character";


      if (s[0].substr(s[0].length - 1, 1) == ".")


              return "period cannot be the last character before the at sign";


      if (!containsOnlyChars(domainChars, s[1]))


              return "invalid character after the at sign";


      var periodIndex = s[1].indexOf(".");


      if (periodIndex == -1)


              return "missing period after the at sign";


      if (periodIndex == 0)


              return "period cannot be the first character after the at sign";


      var periods = s[1].split(".");


      var lastPeriod = periods[periods.length - 1];


      if (lastPeriod.length < 1)


              return "must be at least 1 character after the last period";


      if (!isAlphaNum(s[1].substr(0, 1)))


              return "the first character after the at sign must be alphanumeric";


      if (!isAlphaNum(s[1].substr(s[1].length - 1, 1)))


              return "the last character must be alphanumeric";


      return "";


}







Now, you can take that code and rename it and stuff it into your own Client Script or Script Include, or whatever you'd like.



Oh, they reference a function in there called containsOnlyChars() which can be found on lines 9855 thru 9864:


find_real_file.png


View solution in original post

13 REPLIES 13

rahul_garg
Kilo Expert

You can use isEmailValid(<email to validate>) function at client side to validate it.



For more details search Validation Script in navigator which has more validation scripts.


Hi Rahul,



How can we use those Validation Scripts?


Bob
Giga Contributor

I imagine the other validation scripts are similar using a simple If statement captures a true/false and returns accordingly. I got isEmailValid to work properly by fixing my Error Message. Remember g_form.hideErrorBox("field_name", "Hello World")!


Bob
Giga Contributor

Thanks for the reply Rahul! Problem that I found is that the client script functions don't seem to run in Record Producers? In the code that I showed above, I am trying to use isEmailValid with no success.