Marcellino Ganda
Marcellino Ganda

Reputation: 75

Regex Not working properly c#

String pattern = "([a-zA-Z]{1,2}-[1-9]{1,4}-[a-zA-Z]{2,3})";
    if (CheckMandatory())
    {
        try
        {
            if (ViewState["currentEditDataID"] == null ||         ViewState["currentEditDataID"] == "")
            {
                if (txtNoPolisi.Text.Length < 12 && Regex.IsMatch(txtNoPolisi.Text,pattern))
                {
                   save(); 
                }

i have the pattern above so that the input should be like b-1234-abc / bb-1234-abc

but when i input bbb-1234-asda it doesn't show error

Upvotes: 1

Views: 50

Answers (1)

Eric J.
Eric J.

Reputation: 150238

Sure, it matches the middle of your string

bbb-1234-asda

You need to specify that it needs to match the beginning and end of string to avoid that.

(^[a-zA-Z]{1,2}-[1-9]{1,4}-[a-zA-Z]{2,3}$)

Upvotes: 2

Related Questions