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

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @Ankita9793 , 

To prevent users from entering spaces in the string, you can modify the onChange function to validate the input for spaces. Here's how you can adjust the script:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    
    var ipAddresses = g_form.getValue('enter_ip_addresses');

    // Check for spaces in the string
    if (ipAddresses.trim() !== ipAddresses) {
        g_form.clearValue('enter_ip_addresses');
        g_form.showFieldMsg('enter_ip_addresses', 'Spaces are not allowed in the input', 'error');
        return;
    }

    // Rest of your validation logic goes here
    var ipList = (ipAddresses || '').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;
        }
    });
}

 

 

In this modified script:

  • I've added a check to verify if there are any spaces in the input string using trim().
  • If spaces are found, an error message is displayed, and the input field is cleared.
  • The rest of the validation logic for IP addresses remains unchanged.

This modification ensures that the script checks for spaces in the input string and prevents users from entering spaces in the enter_ip_addresses field.

 

Please mark my answer as Accepted as Solution & hit helpful if it suffices your requirement !!! 

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

Hi @Ankita9793 ,

 

Did you look at my solution 

 

 

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

Hi Sohithanjan,

 

I replied to your post earlier today, not sure why I'm not able to see it.

But in case it was not posted due to some issue, here is my response.

 

Thank you for your response!! but the solution did not work for me, it was accepting the spaces. 
I tried the below logic instead which is working fine for spaces in-between IP addresses but does not throw error for spaces in the beginning of the IP address. If you can please suggest something for that.
ip.PNG

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ipAddresses = g_form.getValue('enter_ip_addresses');
    alert(ipAddresses);
   // var ipList = (ipAddresses || '').replaceAll(/\s/g, '').split(',');
    var ipList = (ipAddresses || '').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])$/;
    //var nospaces = ipAddresses.map(String.trim);
    //alert(+ nospaces);
    //alert('IPadd and IPadd trim' + ipAddresses  +ipAddresses.trim());
    var spacetoCheckreg = /\s/;
    if(spacetoCheckreg.test(ipAddresses)){
           g_form.clearValue('enter_ip_addresses');
                    g_form.showFieldMsg('enter_ip_addresses', 'Please enter valid IP addresses', 'info');
                    //g_form.triggerErrorChangeFieldSet('Please enter valid IP addresses');
                    return true;
    }
    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');
                    //g_form.triggerErrorChangeFieldSet('Please enter valid IP addresses');
                    return true;
                }
       

    });
            }

Harish Bainsla
Kilo Patron
Kilo Patron

Hi try below code

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

// Check if the input contains spaces
if (newValue.trim().includes(' ')) {
g_form.clearValue('enter_ip_addresses');
g_form.showFieldMsg('enter_ip_addresses', 'Spaces are not allowed in the input', 'error');
return;
}

var ipAddresses = g_form.getValue('enter_ip_addresses');
var ipList = (ipAddresses || '').split(',').map(ip => ip.trim()); // Trim each IP to remove leading/trailing spaces

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;
}

for (var i = 0; i < ipList.length; i++) {
if (!ipRegex.test(ipList[i])) {
g_form.clearValue('enter_ip_addresses');
g_form.showFieldMsg('enter_ip_addresses', 'Please enter valid IP addresses', 'info');
return;
}
}
}