How do you limit a variable to 10digits?

Navneet3
Tera Expert

Hi, The requirement is> minimum of 10 digits is required for a variable.

Is this the correct script?

Here is the script. I created a on change script.

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


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

return;

}

 var val = g_form.getValue('Facility_npi');

var pattern = /\d{10}/;

if (pattern.test(val))


alert('looks like 10 digits');


else

find_real_file.png

 

 

find_real_file.png

 

 

 

15 REPLIES 15

Hi Chetan, I got this even thou I entered 10 digit.

find_real_file.png

Hi Navneet,

                   check below i have tried on my instance and its working fine 

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

    var num = g_form.getValue('number'); // add number field value
    var pattern = /^[0-9]{10}$/;

    if (pattern.test(num)) {
        g_form.setValue('number', num);
    } else {
        alert("Please make sure that the number you provide is 10 digits long");
	g_form.clearValue('number');
    }
}

Output : 

 

find_real_file.png

Hi Navneet,

                     If my response with output is worked for you, mark it as correct, so it will be useful for future readers 

Jaspal Singh
Mega Patron
Mega Patron

Since you have already used Validation regex as number all you need is a check for length.

Try below

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var val = g_form.getValue('Facility_npi');
    var lengthis = val.length;
    if (lengthis > 9) { //do nothing
    } else {
        alert('Enter more than 10 digits');
        g_form.getValue('Facility_npi', '');
    }

}

Jaspal, This worked also. thak you