Need to limit to 10 digit number

Johnson13
Tera Expert

I need to limit the field to 10 digit number. Here is my code. need assistance.

 

 

find_real_file.png

Thanks.

6 REPLIES 6

Allen Andreas
Tera Patron

Hi,

You can simply use:

if (newValue != '') {
var nbrwn = g_form.getValue('npi_no');
if (nbrwn.toString().length > 10) {
g_form.clearValue('npi_no');
alert(getMessage("Please enter a valid NPI No.");
 }
}

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Maik Skoddow
Tera Patron

Hi

you have just a little typo and I also recommend removing any white spaces at the beginning and at the end of the user input. 

Line 3: if (!regex.test(newValue) || newValue.length != 10)

Kind regards
Maik

Hitoshi Ozawa
Giga Sage

Johnson,

Fixed the script. I've changed the regex to also check if it's 10 digits or not.

function onChange(control, oldValue, newValue, isLoading) {
    if (newValue != '') {
        try {
            var nbrwn = g_form.getIntValue('npi_no');
            var regex = /^\d{10}$/;
            if (!regex.test(newValue)) {
                alert(getMessage("Please enter a valid NPI No."));
                g_form.clearValue('npi_no');
            }
        } catch (e) {
            alert(e.message);
        }
    }
}

Ankur Bawiskar
Tera Patron

Hi,

you can simply use Variable validation Regex and then link it with that variable

Create New record

find_real_file.png

Regex - ^[0-9]{10}$

find_real_file.png

Then link it with your variable

find_real_file.png

regards
Ankur

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