Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Create a validation in Script Include and call it from Client Script

escanor
Giga Contributor

I had created some validation in Client Script for my page but now i want to create them in Script Include and call it like a function in my Client Script. And have some troubles here ! Example: 

This is my CLient Script for email validate

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	// 	var input = g_form.getValue('email');
	// 	var answer = true;
	// 	var regex = /[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/ig;
		// 		if(!input.match(regex)){
			// 			g_form.addErrorMessage('Email is invalid format ! (abc@domain.com)');
			// 			answer = false;
			// 			g_form.clearValue('email');
			// 			setTimeout(function(){
				// 				g_form.clearMessages();
				// 			}, 5000);
				// 		}else{
					// 			return true;
					// 		}
					var ga = new GlideAjax('emailValid');
					ga.addParam('sysparm_name','isEmail');
					ga.addParam('sysparam_id',newValue);
					ga.getXML(Process);
				}
				function Process(response) {
					
					var answer = true;
					var email = g_form.getValue('email');
					if (!email.match(isEmail(email))){
						g_form.addErrorMessage('Email is invalid format ! (abc@domain.com)');
						answer = false;
						g_form.clearValue('email');
						setTimeout(function(){
							g_form.clearMessages();
						}, 5000);
					}
				}
				
				

 

And here is what i just did with Script include

var emailValid = Class.create();
emailValid.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
	isEmail: function(email){
			var regex = /[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/ig;
			return regex.test(email);
	},
    type: 'emailValid'
});

pls help me solve this !

1 ACCEPTED SOLUTION

Hi escanor,

 

Remove the line  var answer = true and replace with 

 

var ans = response.responseXML.documentElement.getAttribute('answer');

 

if(ans == 'false')

{

 g_form.clearValue('email');

 

g_form.clearMessages();

}

 

View solution in original post

7 REPLIES 7

Happy to hear that enjoy Servicenow.

Nitin Tongrao
Giga Contributor

Hi escanor,

 

Use following code in your client script,

 

var ga = new GlideAjax('Script_Include_Name');

ga.addParam('sysparm_name', 'function_name');

ga.addParam('sysparm_newEmail', newValue);

ga.getXML(process);

 

function process(response)

{

var ans = response.responseXML.documentElement.getAttribute('answer');

if(ans == 'false')

{

alert('Email is not valid.');

}

}

 

 

Thanks,

Nitin Tongrao

Nisha10
Tera Contributor

Hi Nitin Tongrao,

Instead of calling the script include from client script. How can i call it from action of flow designer?