Regex accept numeric only. First character can't be 0

前端 未结 4 1074
长情又很酷
长情又很酷 2020-12-13 20:17

I have a textbox that I created using .NET..

By using that textbox, the user only can key in numeric. But not start with 0. start with 1-9. after the user key in th

4条回答
  •  旧巷少年郎
    2020-12-13 20:56

    As your code is .NET you should not use regex to parse an Integer. Just use UInt32.TryParse() method

    uint num=0;
    if(UInt32.TryParse(str, out num)){
        Console.WriteLine("Converted '{0}' to {1}.", str, num);   
    }else{
        Console.WriteLine("conversion of '{0}' failed.", value==null? "": value);
    }
    

    Old answer

    This simple regular expression will do it ^[1-9]\d*$

提交回复
热议问题