client script to validate characer lenght and allow no special characters.

RudhraKAM
Tera Guru

I have a requirement in which I need to check the length, uniquness and special characters of the variable

1.Length should be max 5 and min 3 

2.No special characters should be allowed

3.and it should check if the prefix already exist or no

(can i write all 3 conditions in 1 on change script ?Because when i give @@@ in the field it is showing the 1st alert (as expected and  also 2nd alert message too.

below is the on change client script i am using 

can some one help me with this 

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	var regex = new RegExp("^[a-zA-Z0-9]*$");   //no special characters
	
	
	if(!regex.test(newValue)){
		
		
		alert('Please enter only letters');
		
		
		g_form.setValue('csv_prefix','');
	}
	
	// calculate length 
	var str = g_form.getValue('csv_prefix');
	var len = str.length;
	if(len> 5 || len< 3)
		{
		g_form.setValue('csv_prefix','');
		alert('Please enter atleast 3 and maximum of 5');
	}
	
}

 

This is the client script which i copied from community which is not working either 

If abc  name is present in the tables it should alert and clear off the message 

 

function onChange(control, oldValue, newValue, isLoading) {
	if (!isLoading && newValue !='')
	{
		var gr = new GlideRecord('u_delegated_management_environment');                                 // read from Configuration Item table
		var uname = g_form.getValue('csv_prefix');
		gr.addQuery('name', uname);                                                   // prepare query
		gr.query(function(rec){
			if (rec.next()) {                                                               
				alert('Name ' + uname + ' already exists. Assign an other but unique name, please.');
				g_form.setValue('csv_prefix','');                               // then delete name input field
			} else {
				if (uname != g_form.getValue('csv_prefix')) {       
					g_form.setValue('csv_prefix',uname);                 
				}
				alert('Name ' + uname + 'does not exist. Accepted');
			}
		});
	}
}
1 ACCEPTED SOLUTION

Hello,

Regarding your last question, yes we can add two different message for special character and exceed length,

I have updated your script mentioned in the question,

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var regex = new RegExp("^[a-zA-Z0-9]*$"); //no special characters
var str = g_form.getValue('csv_prefix');
var len = str.length;

if(!regex.test(newValue))

{
alert('Please enter only letters');
g_form.setValue('csv_prefix','');
}
else if(len> 5 || len< 3)
{
alert('Please enter atleast 3 and maximum of 5');
g_form.setValue('csv_prefix','');
}

Let me know if you still have any doubts.

Mark it as helpful or correct, it it really helps.

Thanks.

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

View solution in original post

7 REPLIES 7

can you help me with the script please.

Thanks for the script ggg 

 

one last question if i enter ** in the field it is first check the character length , can we write some thing like if  any special characters added then alert about special characters and if length exceed then length alert.

Hello,

Regarding your last question, yes we can add two different message for special character and exceed length,

I have updated your script mentioned in the question,

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var regex = new RegExp("^[a-zA-Z0-9]*$"); //no special characters
var str = g_form.getValue('csv_prefix');
var len = str.length;

if(!regex.test(newValue))

{
alert('Please enter only letters');
g_form.setValue('csv_prefix','');
}
else if(len> 5 || len< 3)
{
alert('Please enter atleast 3 and maximum of 5');
g_form.setValue('csv_prefix','');
}

Let me know if you still have any doubts.

Mark it as helpful or correct, it it really helps.

Thanks.

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP