Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

ip validation .net core

public class IPAddressAttribute : ValidationAttribute
{

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        YourViewModel yourviewmodel = (YourViewModel)validationContext.ObjectInstance;

        const string regexPattern = @"^([d]{1,3}.){3}[d]{1,3}$";
        var regex = new Regex(regexPattern);
        if (string.IsNullOrEmpty(yourviewmodel.IpAddress))
        {
            return new ValidationResult("IP address  is null");
        }
        if (!regex.IsMatch(yourviewmodel.IpAddress )|| yourviewmodel.IpAddress.Split('.').SingleOrDefault(s => int.Parse(s) > 255)!=null)
        return new ValidationResult("Invalid IP Address");


        return ValidationResult.Success;
    }
}

// use
[IPAddressAttribute]
[Display(Name = "IP Address")]
public string IpAddress { get; set; }
Comment

PREVIOUS NEXT
Code Example
Csharp :: C# accesseurs 
Csharp :: c# dictionary contain key but returns false 
Csharp :: c# psobject get value 
Csharp :: unity make particles stay still 
Csharp :: AutoFixture ignore property 
Csharp :: c# execute run control panel 
Csharp :: unity rotatoin angle 
Csharp :: c# extension method in non static class 
Csharp :: c# file watcher specific file 
Csharp :: wpf open new Window in MVVM 
Csharp :: multiple lines in string c# parameterized 
Csharp :: get first and last item list c# 
Csharp :: C# verify "no other" call xunit 
Csharp :: C# count specific words in string 
Csharp :: translate nicely between two vector3 
Csharp :: c# hashset 
Csharp :: c# escape quotes 
Csharp :: minimum value int C# 
Csharp :: unity new input system get button down 
Csharp :: what is c# used for 
Csharp :: c# arraylist to listview 
Csharp :: width="331" height="331" 
Csharp :: flsa itextsharp 
Html :: html yen symbol 
Html :: html starter code 
Html :: HP cmd get computer serial number 
Html :: disable html form input autocomplete autofill 
Html :: how to add title icon in html 
Html :: lock the zoom html 
Html :: laravel blade remove all html tags 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =