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

ggg
Giga Guru

I believe this is what you are looking for:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    //var regex = new RegExp("^[a-zA-Z0-9]*$");   //no special characters
    var regex = new RegExp("[a-zA-Z0-9]");
    
    var str = g_form.getValue('u_title');
    var len = str.length;
    if(len  < 6 && len > 2){
        alert('1');
        if(regex.test(newValue)){
            alert('2');
        }
        else {
            alert('Please enter only letters or numbers');
            g_form.setValue('u_title','');
        }
    }
    else {
        g_form.setValue('u_title','');
        alert('Please 3 - 5 characters.');
    }
}

can we include unique name script in this one too ?

I do not understand your question ... unique name script??

1) put the unique name validation in its own on change client script

2) i have never used the construct you have in your name validation script and when i test it i get the same wrong result as you do.

    I would use an ajax script to determine if the value is unique.