Client script to restrict specials characters

Kiran Dibakar
Kilo Contributor

hi

i have one text field on service catalog item on which i have to do two validations:-

1. only accept ip address in their correct format

2. if more than 1 it should automatically insert comma 

for eg. ip address1, ip adddress 2

and it should only allow comma for this no other characters.

thanks in advance!!

 

1 ACCEPTED SOLUTION

Then the following will validate the entry. I've named the field "ip_address".

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var pattern = /^((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(,(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))*$/;
    if (!pattern.test(newValue)) {
        g_form.showFieldMsg("ip_address", "Invalid ip address. IP address should be delimited by a comma", "error");
    }
}

Execution result:

find_real_file.png

Error because 2nd ip address delimited by ";"

find_real_file.png

View solution in original post

3 REPLIES 3

Hitoshi Ozawa
Giga Sage
Giga Sage

Not clear about requirement 2. How will the user input ip addresses?

Will user enter multiple ip address separated by comma? In this case, user will be entering the comma and not the script.

yes user will enter comma if they use extra spaces or other special character it should not accept

eg:

1. ip_address1,ipaddress2,... - accepted

2. ip_address1  # ipaddress2     ip address3 - not accepted

 

Then the following will validate the entry. I've named the field "ip_address".

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var pattern = /^((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(,(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))*$/;
    if (!pattern.test(newValue)) {
        g_form.showFieldMsg("ip_address", "Invalid ip address. IP address should be delimited by a comma", "error");
    }
}

Execution result:

find_real_file.png

Error because 2nd ip address delimited by ";"

find_real_file.png