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

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


Bob
Giga Contributor

That is the function I was trying to call from within the context of a record producer, but for whatever reason you can't use it there? Can you call global Client Scripts from within Record Producer?


Ah, a record producer... there is one sure-fire way:   create your function within the record producer itself.     However, that's icky as it'd benefit more from a global source.



Because a record producer's code is all handled Server Side, I'm assuming you want these checks on Submit?   If you want the email validation to take place when the field changes (onchange), I'm not sure how to accomplish that in a record producer.   I'm going to try to find out though because now my curiosity has been piqued.


Bob
Giga Contributor

It worked David! Just totally messed up the basic errorbox. I have it figured out now and thanks a bunch for the education behind the function! I am going to try to use the return for the isEmailValidWithReason to give the user a more defined reason as well!