Search code examples
.netregexasp.net-coreannotations

Can someone help me with Regular Expression in .net


Can someone help me modify this

[RegularExpression(@"([0-9]+)", ErrorMessage = "Must be a Number.")] 

to become like this form. Example 12345678X eight numbers and the last char is an alphabetic character.


Solution

  • I hope this will solve your issue

    [Test]
    public void RegexTest()
    {
        var regex = new Regex("^[0-9]{8}[A-Za-z]$");
        Assert.IsTrue(regex.IsMatch("12345678A"));
        Assert.IsFalse(regex.IsMatch("12345678AB"));
    }