-1

How to Validate numerical input, max length limited 3 and should not start with zero characters (010, 001 should invalidate). I used C# regex.IsMatch() with following regex ([1-9]|[1-9][0-9]|[1-9][0-9][0-9])*. But it validating inputs start with zeros. How to resolve this..?

vahdet
  • 6,357
  • 9
  • 51
  • 106
  • 2
    Can you post a [MCVE] with a list of input? – aloisdg Apr 01 '19 at 11:42
  • What do you think the asterisk `*` is doing there in your Regex? Also, if your regex should match from the beginning of the string/line, anchor the regex at the start of the string by using the `^` anchor as first character in your regex. –  Apr 01 '19 at 11:45
  • 1
    Try this => `bool isMatch = Regex.IsMatch("100", @"^[1-9][0-9]{0,2}$");` – er-sho Apr 01 '19 at 11:56
  • The input numerical, max length 3, should not start with zeros like 010, 001, 05 `private bool Validate(string str) { bool isValid = true; Regex rx = new Regex("^([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$*"); isValid = rx.IsMatch(str); return isValid; }` – Kasun Amarasinghe Apr 01 '19 at 12:14
  • @KasunAmarasinghe Can you add that code to the question? – The fourth bird Apr 01 '19 at 12:16

5 Answers5

4

You can omit the alternations by repeating a character class 0 - 2 times and you should use anchors to assert the start ^ and the end $ of the string.

^[1-9][0-9]{0,2}$
  • ^ Start of string
  • [1-9] Match a digit 1-9
  • [0-9]{0,2} Match 0, 1 or 2 times a digit 0-9
  • $ Assert end of the string

Usage:

bool isMatch = Regex.IsMatch("100", @"^[1-9][0-9]{0,2}$");

Regex demo

er-sho
  • 9,581
  • 2
  • 13
  • 26
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • 2
    @elgonzo https://stackoverflow.com/questions/16621738/d-is-less-efficient-than-0-9 – The fourth bird Apr 01 '19 at 11:56
  • 1
    @elgonzo Try with `๔๕๖๗๘๙໐໑-௫௬௭௮` – aloisdg Apr 01 '19 at 11:57
  • Dang! You guys got me. You are correct. It totally escaped me that there are numeric characters other than 0...9 in different languages. Doh! What a massive brainfart ;-) –  Apr 01 '19 at 11:59
  • 1
    @Thefourthbird, I planed to add my answer but later I think its better to add it to your, if you found well then keep it or otherwise rollback to your version :) – er-sho Apr 01 '19 at 12:00
1

Regex are great sure. Note that you could achieve this easily without any regex:

static bool IsValid(string input)
{
    return !string.IsNullOrEmpty(input)
        && input.Length < 3 && !input.StartsWith("0") && input.All(char.IsDigit); 
}

Try it online

aloisdg
  • 22,270
  • 6
  • 85
  • 105
1

I'd be more specific with this one:

^((?!(0))[0-9]{0,3})$ 

Explanations:

  • 1st one is not 0 & i want maximum 3 digits between 0 and 9.
Razvan Dumitru
  • 11,815
  • 5
  • 34
  • 54
1

[1-9][0-9]{2}$

This would work as per your requirement. testes on regex buddy with following test cases

  1. 001 Fail
  2. 1000000 Fail
  3. 900 Pass
  4. 010 fail
0

You can use ^[1-9][0-9]{0,2}$ it won't allow starting with zero

Aderbal Farias
  • 989
  • 10
  • 24