web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Format IP Address to include leading 0's - JavaScript

(0) ShareShare
ReportReport
Posted on by 125

I'm currently attempting to implement code that will take an IP address from one field, format it to include leading zeros, and then push that new format to a different field for sorting.  I'm really new to coding and I'm having a difficult time trying to figure this out.

Example of what I'm expecting:

Field one value = 10.240.1.15

Format string and insert into Field2 = 010.240.001.015


I have it working in JSFiddle, but I'm having issues transferring this into Dynamics CRM.

function IPsort() {
    var ipAddress = Xrm.Page.getAttribute("new_ipaddress").getValue();
    var ipSort = Xrm.Page.getAttribute("new_ipsort");
    function paddy(n, p, c) {
        var pad_char = typeof c !== 'undefined' ? c : '0';
        var pad = new Array(1 + p).join(pad_char);
        return (pad + n).slice(-pad.length);
    }
    var num = paddy(ipAddress.split(".", 1)[0], 3) + '.' +
        paddy(ipAddress.split(".", 3)[1], 3) + '.' +
        paddy(ipAddress.split(".", 3)[2], 3) + '.' +
        paddy(ipAddress.split(".", 4)[3], 3);
    ipSort.setValue(num);
}
Any help would be greatly appreciated.

Thanks!

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Nuno Profile Picture
    2,005 on at

    Hi,

    Try this function to get your string formatted the way you want:

    function GetIP(var str) {
        var res = str.split('.');
    	
    	for(var i= 0; i < res.length; i++)
    	{
    	if(res[i].length == 1)
    	{
    		res[i] = "00" + res[i];
    	}
    	else if(res[i].length == 2)
    	{
    		res[i] = "0" + res[i];
    	}
    	}
        return res.join('.');
    }
  • Nate Arms Profile Picture
    125 on at

    Much cleaner code... thanks for responding so quickly.  I got everything working now too, thanks!

    In case anyone wants to see the final code, it's here

    function formatIP()
    {
        var ipAddress = Xrm.Page.getAttribute("new_ipaddress").getValue();
        var ipSort = Xrm.Page.getAttribute("new_ipsort");
        function GetIP(str) {
            var res = str.split('.');
            for (var i = 0; i < res.length; i++) {
                if (res[i].length == 1) {
                    res[i] = "00" + res[i];
                }
                else if (res[i].length == 2) {
                    res[i] = "0" + res[i];
                }
            }
            return res.join('.');
        }
        var str = GetIP(ipAddress);
        ipSort.setValue(str);
        ipSort.setSubmitMode("always");
    }


Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans