Modifying the Infor CRM SLX Web Client Email Control’s Input Validation

In a previous blog post I detailed how to extend the Infor CRM SLX email control to have different validation rules about the kind of data that is considered a valid email address. Previously the email control had pretty rudimentary validation, only enforcing there was something before and after an @ symbol. Starting in version 8.5.0 the validation has improved. With this improvement, the way of altering the validation function has also changed from what I had previously documented. Previously the /Model/Portal/SlxClient/SupportFiles/jscript/Sage/UI/Controls/Email.js file itself had a function for the email validation. However in 8.5 the validation within the Email.js file has been removed and the validation now exists in the /Model/Portal/SlxClient/SupportFiles/jscript/Sage/Utility.js. Within the Utility.js file there is a function called emailAddressRegEx which now handles the validation of email addresses. It now validates the data before the @ symbol, and also validates that the suffix or host is valid.

		emailAddressRegEx: function(flags){
			// summary:
			//		Builds a regular expression that matches an email address
			// flags: Object?
			//		- flags.allowCruft  Allow address like `<mailto:foo@yahoo.com>`.  Default is false.
			//		- flags in regexp.host can be applied.
			//		- flags in regexp.ipAddress can be applied.

			// assign default values to missing parameters
			flags = (typeof flags == "object") ? flags : {};
			if (typeof flags.allowCruft != "boolean") { flags.allowCruft = false; }
			flags.allowPort = false; // invalid in email addresses

			// user name RE per rfc5322
			var usernameRE = "([!#-'*+\\-\\/-9=?A-Z^-~]+[.])*[!#-'*+\\-\\/-9=?A-Z^-~]+";

			// build emailAddress RE
			var emailAddressRE = usernameRE + "@" + Sage.Utility.fnHost(flags);

			// Allow email addresses with cruft
			if ( flags.allowCruft ) {
				emailAddressRE = "<?(mailto\\:)?" + emailAddressRE + ">?";
			}

			return emailAddressRE; // String
		},
		fnHost: function(flags){
			// summary:
			//		Builds a RE that matches a host
			// description:
			//		A host is a named host (A-z0-9_- but not starting with -), a domain name or an IP address, possibly followed by a port number.
			// flags: Object?
			//		- flags.allowNamed Allow a named host for local networks. Default is false.
			//		- flags.allowIP  Allow an IP address for hostname.  Default is true.
			//		- flags.allowLocal  Allow the host to be "localhost".  Default is false.
			//		- flags.allowPort  Allow a port number to be present.  Default is true.
			//		- flags in regexp.ipAddress can be applied.

			// assign default values to missing parameters
			flags = (typeof flags == "object") ? flags : {};

			if(typeof flags.allowIP != "boolean"){ flags.allowIP = true; }
			if(typeof flags.allowLocal != "boolean"){ flags.allowLocal = false; }
			if(typeof flags.allowPort != "boolean"){ flags.allowPort = true; }
			if(typeof flags.allowNamed != "boolean"){ flags.allowNamed = false; }

			//TODO: support unicode hostnames?
			// Domain name labels can not end with a dash.
			var domainLabelRE = "(?:[\\da-zA-Z](?:[-\\da-zA-Z]{0,61}[\\da-zA-Z])?)";
			var domainNameRE = "(?:[a-zA-Z](?:[-\\da-zA-Z]{0,252}[\\da-zA-Z])?)"; // restricted version to allow backwards compatibility with allowLocal, allowIP

			// port number RE
			var portRE = flags.allowPort ? "(\\:\\d+)?" : "";

			// build host RE
			var hostNameRE = "((?:" + domainLabelRE + "\\.)+" + domainNameRE + "\\.?)";
			if(flags.allowIP){ hostNameRE += "|" +  regexp.ipAddress(flags); }
			if(flags.allowLocal){ hostNameRE += "|localhost"; }
			if(flags.allowNamed){ hostNameRE += "|^[^-][a-zA-Z0-9_-]*"; }
			return "(" + hostNameRE + ")" + portRE; // String
		
        }

That is a lot of validation. Ultimately what is being returned is a regex expression that the user input is tested against. It the input matches the regex pattern then it is a “good” email address. If not, then the validation fails. Luckily we don’t have to modify all of that code if we don’t want to. Instead we can just replace that function with our own. If we ever want to back ours out then it would revert to the standard validation.

To overwrite the standard function we don’t need to replace or append to the Utility module. We can just override the specific function. The easiest way to do this is to use the Customer FX custom loader and add a new module or script file. Then inside that new file, we simply access the existing Sage.Utility object and replace the emailAddressRegEx function like this:

define([],
	function () {			
            Sage.Utility.emailAddressRegEx = function (flags) {          				                   
                var validationString = ".*";
                return validationString;
            };            
        }
);

This is a very simple example, that returns a regex string that will match anything, which essentially negates any validation of the email control. But hopefully you get the point.

Want content like this delivered to your inbox? Sign up for our newsletter!
ABOUT THE AUTHOR

Kris Halsrud

Kris Halsrud is a Senior Analyst / Developer for Customer FX Corporation.

Submit a Comment

Your email address will not be published. Required fields are marked *