onChange client script to require 16 characters in a variable field

DTrishman
Tera Contributor

Good morning,

 

I have a requirement to require exactly 16 characters on a catalog item field to help ensure end users submit a full debit or credit card number with fewer errors. I'm not sure if I need to target a table and field name in addition to identifying the field names in the code.

I would also like this to work on three additional fields named "card_number_2", "card_number_3", and "card_number_4".

 

Any help fixing this code would be much appreciated!

 

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


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

    }

  //Type appropriate comment here, and begin script below


  //Require at least 10 characters in the 'Comments' field


  var maxLength = 16;


  var control = g_form.getControl('card_number');


  if(control.value.length < maxLength){


  g_form.hideErrorBox('card_number');


  g_form.showErrorBox('card_number', 'You must type exactly ' + maxLength + ' characters in this field. You have entered characters less than ' + maxLength);


  return false;


  }


  else if(control.value.length > maxLength){


  g_form.hideErrorBox('card_number');


  g_form.showErrorBox('card_number', 'You must type exactly ' + maxLength + ' characters in this field. You have entered characters more than ' + maxLength);


  return false;


  }


  else{


  g_form.hideErrorBox('card_number');


  }

		
}

 

1 ACCEPTED SOLUTION

Danish Bhairag2
Tera Sage
Tera Sage

Hi @DTrishman ,

 

Please use the below code to validate if user has entered 16 digits or not. You can create same onChange client script for other fields(card_number2,card_number3,card_number4).

 

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

    //Type appropriate comment here, and begin script below
    alert(newValue.length);//later comment the alert;
    if (newValue.length == 16) {
        //Dont do anything
    } else {
        g_form.showFieldMsg('card_number', 'Total 16 digits should be entered.Kindly Re-Enter', 'error');//update the message as desired 
    }

}

 

DanishBhairag2_0-1695907845234.png

 

Please mark my answer helpful & accepted if it helps you resolve your query.

 

Thanks,

Danish

View solution in original post

8 REPLIES 8

Danish Bhairag2
Tera Sage
Tera Sage

Hi @DTrishman ,

 

Please use the below code to validate if user has entered 16 digits or not. You can create same onChange client script for other fields(card_number2,card_number3,card_number4).

 

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

    //Type appropriate comment here, and begin script below
    alert(newValue.length);//later comment the alert;
    if (newValue.length == 16) {
        //Dont do anything
    } else {
        g_form.showFieldMsg('card_number', 'Total 16 digits should be entered.Kindly Re-Enter', 'error');//update the message as desired 
    }

}

 

DanishBhairag2_0-1695907845234.png

 

Please mark my answer helpful & accepted if it helps you resolve your query.

 

Thanks,

Danish

This worked great for what I'm looking for. Thank you!

RAMANA MURTHY G
Mega Sage
Mega Sage

Hello @DTrishman ,

You can use the RegExp for this. Find below code

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
	var cardRegEx = /^\d{16}$/;
	var cardNum = g_form.getValue('u_debit_credit_card_number')
	if(!cardRegEx.test(cardNum)){
		g_form.setValue(control.name,'');
		g_form.showFieldMsg('u_debit_credit_card_number','Enter valid 16 digits Debit/Credit card number','error');
	}
}

 

 

This code also restricts users from entering alphabets, and any special symbols (including spaces). This RegExp only accepts digits exactly 16 characters. 

 

 

Please mark my answer correct & Helpful, if it helps you

Thank you

 

 

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

G Ramana Murthy
ServiceNow Developer

Vishal Birajdar
Giga Sage

Hi @DTrishman 

 

You can use RegEx instead of writing script on each variable.

As its single line text, user may enter 'characters' also so its better to validate on numbers only.

 

In Navigator >> Go To >> Service catalog >> catalog variables>>  Variable Validation regex 

 

Create new record as below 

 

VishalBirajdar_0-1695963273139.png

 

Use this newly created Regex in variable

VishalBirajdar_1-1695963333252.png

 

Output : 

VishalBirajdar_2-1695963412476.png

 

 

 

 

 

 

 

 

 

 

 

Vishal Birajdar
ServiceNow Developer

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