Restrict spaces in IP address
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-29-2024 10:48 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-29-2024 11:40 PM
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 !!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2024 10:31 PM
Hi @Ankita9793 ,
Did you look at my solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2024 10:50 PM - edited 03-04-2024 10:54 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2024 09:47 PM
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;
}
}
}