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

JerryJ071847183
Tera Sage

Hi escanor,

 

Something like below is missing from your client script under Function Process(response)

 


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

 

Take the below sample as reference for client script.

var glaj = new GlideAjax('learn');

glaj.addParam('sysparm_name','passion');
glaj.addParam('sysparm_dimple',newValue);
glaj.getXML(callkaru);

function callkaru(response)
{

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


}

 

 

And I also want to know that why are you validating two times once in Script Include and Once in client script 

by using that IF loop under process function .

 

 

PLEASE let me know your response.

 

Mark my ANSWER as CORRECT if it helped you in achiveing sol.

 

I want to create in Script Include so another one can call my function to use in another page in the same project

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();

}

 

Ty bro ! it's worked !