Validation on field with below combination

Deepika Mishra
Mega Guru

I have a requirement to be implemented for a field. For which I need to implement onChange script with below validation:

Alphanumeric combination of 14 digits where the value should start with "A" and only "-" is allowed as special character.

Please help me in implementing this requirement.

1 ACCEPTED SOLUTION

Hello @Deepika Mishra 

Please try below code segment

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var regex = /^A[A-Za-z0-9-]{13}$/;
   var name = g_form.getValue('u_name');
   if(regex.test(name) == false){
	g_form.showFieldMsg('u_name','String should start with A and must contain 14 digits, Hyper (-) only allowed','error');
   }
}

 

 

 

Please mark my answer helpful, if it helps you

Thank you

 

 

 

Please mark my answer helpful  & correct if it helps you
Thank you

G Ramana Murthy
ServiceNow Developer

View solution in original post

5 REPLIES 5

Vishal Birajdar
Giga Sage

Hi @Deepika Mishra 

 

Make use of ChatGPT know

 

here is the output : 

 

^A[a-zA-Z0-9-]{13}$

'A' - Match character A

[a-zA-Z0-9-] - match any alpha numeric character or -

{13} - preceding character

 

Client Script : 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

   if (isLoading || newValue === '') {

      return;

   }

   var regex = /^A[a-zA-Z0-9-]{13}$/;

   if(!regex.test(newValue)){

    g_form.showFieldMsg("Your field name","Please enter valid character",Error);

   } else {
//g_form.clearValue('Your field name');
    g_form.clearMessages();

   }

}

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates