The CreatorCon Call for Content is officially open! Get started here.

onChange Client Script for maximum number of digits

Bruce11
Giga Expert

Hi,

I have an onChange client script that is not working the way it should. I am trying to notify users that the maximum numbers allowed is 10 digits (if they are trying to enter more than 10). I also want the variable to allow only integer. Below is what i have but its not working properly.

 

var parsed = g_form.getIntValue("variable_name");

//if "Not a Number", clear the parsed value

if (isNaN(parsed)){

parsed = "";

}

if (parsed != newValue) {

alert("Please enter a valid integer number ");

g_form.setValue("variable_name", parsed);

//if more than 10
if(parseInt(newValue) > 10){
alert(getMessage("10 digits max"));
g_form.setValue("variable_name", '');
return false;
}
}

}

 

Thank you.

1 ACCEPTED SOLUTION

Amar Kutlaria
Mega Guru

You can try onchange script provided by Kunal or

You can use variable attributes to set the max_length on the variable to stop user from entering more than 10 characters. Then, you use on change client script to validate entered characters are integers only.

 

find_real_file.png

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

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

  return;

  }

  var regexp = /^([0-9]+[0-9])/;

  if(!regexp.test(newValue)) {

  alert('Please enter a 10 digit number');

  g_form.setValue('variable name',''); // set variable to empty

  }

}

 

If I answered your question, please mark Correct/Helpful.

View solution in original post

5 REPLIES 5

Manoj Kumar16
Giga Guru

Put the below code in your onchange client script

var regex= /^[0-9]*$/;

if(!regex.test(newValue) || (newValue.length <= 9) )// adjust your digits.

{

alert('please enter only digits and min length should be 10 ');

g_form.setValue('variable_name', ''); // Blank Out the field

}

 

Kunal Varkhede
Tera Guru

Hi Bruce,

 

Refer Below script

var isnum = /^\d+$/.test(g_form.getValue('variable_name'));
//alert("isnum: " + isnum);
if (!isnum) {
alert('variable_name should be only numbers.');
return false;
} else {
if (g_form.getValue('vaiable_name').length > 10) {
alert('variable_name should not contain more than 10 digits.');
return false;
}
}
}

Please Mark Correct/Helpful if it Help you.

Regard,

Kunal.

Amar Kutlaria
Mega Guru

You can try onchange script provided by Kunal or

You can use variable attributes to set the max_length on the variable to stop user from entering more than 10 characters. Then, you use on change client script to validate entered characters are integers only.

 

find_real_file.png

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

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

  return;

  }

  var regexp = /^([0-9]+[0-9])/;

  if(!regexp.test(newValue)) {

  alert('Please enter a 10 digit number');

  g_form.setValue('variable name',''); // set variable to empty

  }

}

 

If I answered your question, please mark Correct/Helpful.

This worked well. 

Thank you all for your help.