- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2023 05:51 AM
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');
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2023 06:32 AM
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
}
}
Please mark my answer helpful & accepted if it helps you resolve your query.
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2023 06:32 AM
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
}
}
Please mark my answer helpful & accepted if it helps you resolve your query.
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2023 08:09 AM
This worked great for what I'm looking for. Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2023 06:32 AM - edited 09-28-2023 06:33 AM
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
Thank you
G Ramana Murthy
ServiceNow Developer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2023 09:58 PM
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
Use this newly created Regex in variable
Output :
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates