Validate two IP addresses in a single line text field

Abhishek Barik
Tera Contributor

Hi Team,

We have below requirement to validate two IP address. I have wrote for one but couldn't think of including two validations:

Requirement:
the user should enter one or two IP addresses separated by a single space. Then the form should validate the one or two IP addresses. If both valid, the field keeps the values, otherwise if one or more is invalid, delete the invalid IPs and ask the user to enter the failed IP again. 

Single IP address validation onChange client script:

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

    if (/^(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]?)\.(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]?)$/.test(newValue)) {
        return (true);
    } else {
        g_form.setValue('abc', ''); //where abc is the field for validating IP addresss
        alert("'" + newValue + "' is not a valid IP address. Please enter a valid IP address.");
    }

}

Please suggest the script for above requirement.

 

Thanks in advance,

Abhishek

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Following will validate 2 ip address. If one or both is invalid, it will delete the invalid address and display a message.

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]?)\.(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]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;

    var ipAddressList = newValue.split(' ');
    var validIpList = [];
    var errFlg = false;
    for (var i = 0; i < ipAddressList.length; i++) {
        if (pattern.test(ipAddressList[i])) {
            validIpList.push(ipAddressList[i]);
        } else {
            errFlg = true;
        }
    }
    if (errFlg) {
        g_form.setValue('ip_address', validIpList.join(' '));
        g_form.showFieldMsg('ip_address', 'There was an invalid ip address. Please re-enter.', 'error');
    } else {
        g_form.hideFieldMsg('ip_address');
    }
}

 

View solution in original post

4 REPLIES 4

Tadz
Tera Guru
Tera Guru

Hi, 

You could make use of the OOB Variable Validation Regex

Here is a sample of what i created.

  1. Navigate to Service Catalog > Catalog Variables > Variable Validation Regex.
  2. Click New.

find_real_file.png

 

aIn my catalog variable item

select the regular expression created for IP Address.

find_real_file.png

Result:

Invalid result:
find_real_file.png

valid result:

find_real_file.png

 

Note I'm using Simple regex to check for an IP address:

^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$

please see this regular expression link for reference.

Hitoshi Ozawa
Giga Sage
Giga Sage

Following will validate 2 ip address. If one or both is invalid, it will delete the invalid address and display a message.

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]?)\.(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]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;

    var ipAddressList = newValue.split(' ');
    var validIpList = [];
    var errFlg = false;
    for (var i = 0; i < ipAddressList.length; i++) {
        if (pattern.test(ipAddressList[i])) {
            validIpList.push(ipAddressList[i]);
        } else {
            errFlg = true;
        }
    }
    if (errFlg) {
        g_form.setValue('ip_address', validIpList.join(' '));
        g_form.showFieldMsg('ip_address', 'There was an invalid ip address. Please re-enter.', 'error');
    } else {
        g_form.hideFieldMsg('ip_address');
    }
}

 

Execution result:

1. Enter "192.168.2.2 192.168.1.256"

find_real_file.png

2. Enter "192.168.2.2 192.168.1.2"

find_real_file.png

veerasivareddy
Tera Contributor

Hi Hitoshi Ozawa,

Can we separate IP address with comma?