Restrict spaces in IP address

Ankita9793
Tera Contributor

Hi All

 

Can someone please help with below script, how can I validate whenever user enters spaces(beginning, end, inbetween) in the string. Should not allow user to enter spaces.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ipAddresses = g_form.getValue('enter_ip_addresses');
    var ipList = (ipAddresses || '').replaceAll(/\s/g, '').split(',');
   
    var ipRegex = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/;

    if (ipList.length > 30) {
        g_form.clearValue('enter_ip_addresses');
        g_form.showFieldMsg('enter_ip_addresses', 'Maximum 30 IP addresses are allowed', 'info');
        return;
    }

    ipList.some(function (item) {
        if (!ipRegex.test(item)) {
            g_form.clearValue('enter_ip_addresses');
            g_form.showFieldMsg('enter_ip_addresses', 'Please enter valid IP addresses', 'info');
            return true;
        }
    });
}
8 REPLIES 8

Pravindra1
Tera Guru

You can try OOB Validation regex in variable. If this field is not available in your instance then you can pull it to the form using form layout configuration.

Pravindra1_0-1709276554859.png

 

Hi @Ankita9793 ,

 

Avoid doing customization when the functionality already exist.

 

Go with what @Pravindra1 has recommended above... its simple and no code solution... 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Hi Sohail,

 

The OOB solution does not fulfill my requirement.

Instead of creating a client script do a validation , you can create a regex rule and use it for validation as did above for IP address validation.

 

try this regex to avoid spaces > pattern = /^[0-9a-zA-Z]*$/;

Refer how IP Address regex is defined and use that as a base to create your own regex..

 

i hope this helps @Ankita9793 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect