Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Validating Text Field in IP address Format

(0) ShareShare
ReportReport
Posted on by 332

I have a requirement to validate a text field to an IP address format,

I have seen the java script , and i have created the web resource but the validation fails even when I type a correct IP address. Here is the code below

function format_ipaddress(fieldName)
{
	 var ipaddress = Xrm.Page.data.entity.attributes.get(fieldName).getValue();
     var ipformat =/^(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]?)$/; 	
		
	if ((ipaddress != "") && (ipaddress != null))
	{
	   if (ipaddress == ipformat) 
	{
		ipaddress = ipaddress;
	}
		else 
		{
			alert ("Invalid IP address format");
			ipaddress = "000.000.000.000";	
		}
		
		Xrm.Page.data.entity.attributes.get(fieldName).setValue(ipaddress);
	}
}


Even if I type a correct IP address i still get an Invalid Ip address format.

Is something wrong with the regex expression.

Regards.

*This post is locked for comments

  • lawix10 Profile Picture
    lawix10 332 on at
    RE: Validating Text Field in IP address Format

    Thanks this was the right answer.

    Regards.

  • lawix10 Profile Picture
    lawix10 332 on at
    RE: Validating Text Field in IP address Format

    Thanks, but I went for another method

  • Verified answer
    Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,401 on at
    RE: Validating Text Field in IP address Format
    Hi
    Please try the following:


    function format_ipaddress(fieldName) {
        var ipaddress = Xrm.Page.data.entity.attributes.get(fieldName).getValue();
        var ipformat = /^(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]?)$/;
    
        if ((ipaddress != "") && (ipaddress != null)) {
            if (!ipaddress.match(ipformat)) {
                Xrm.Utility.alertDialog("Invalid IP address format", null)
                ipaddress = "000.000.000.000";
                Xrm.Page.data.entity.attributes.get(fieldName).setValue(ipaddress);
            }
        }
    }







  • Verified answer
    M.Azwar Alam Profile Picture
    M.Azwar Alam on at
    RE: Validating Text Field in IP address Format

    Hi

    You can check following method.I got it from following blog.

    www.w3resource.com/.../ip-address-validation.php

    1. function ValidateIPaddress(inputText)  
    2.  {  
    3.  var ipformat = /^(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]?)$/;  
    4.  if(inputText.value.match(ipformat))  
    5.  {  
    6.  document.form1.text1.focus();  
    7.  return true;  
    8.  }  
    9.  else  
    10.  {  
    11.  alert("You have entered an invalid IP address!");  
    12.  document.form1.text1.focus();<br>return false;  
    13.  }  
    14.  }  

    Mark asnwer as verified, if it works for you.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,432 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans