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

Ankur Bawiskar
Tera Patron
Tera Patron

@Deepika Mishra 

should be an easy with regular expression.

you can search related to this on google or regex sites

what did you try so far and what didn't work?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Deepika Mishra
Mega Guru

I want to how the combination of condition should be
I need help me in that, taking out regex is not big thing but extracting "-" and adding condition to it is.

So there I need help me in understanding

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

Harish Bainsla
Kilo Patron
Kilo Patron

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}


var pattern = /^A[A-Za-z0-9]{12}-[A-Za-z0-9]$/;

if (!pattern.test(newValue)) {

g_form.showFieldMsg(control.name, "Value should start with 'A' and be 14 characters long (including '-')", "error");

g_form.setValue(control.name, '', '');
} else {

g_form.hideFieldMsg(control.name);
}
}