none
Problem to keep Alphanumeric

    Question

  • Hi,
    I want to detect the given input string is alphanumeric, with only - and _ inside. How to complete the following?

    var matches = Regex.Matches(text, @"^[a-zA-Z0-9-_]+$");



    Many Thanks & Best Regards, Hua Min

    Friday, April 7, 2017 6:33 AM

Answers

All replies

  • try

    var matches = Regex.Matches(value, @"^[A-Za-z_-][A-Za-z0-9_-]*$");
    


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Friday, April 7, 2017 6:55 AM
  • Thanks.
    I mean how I can finish the above part, to show/detect in case that given text is falling out of this condition.

    Many Thanks & Best Regards, Hua Min

    Friday, April 7, 2017 7:01 AM
  • try if following works for you
    private bool isValid(String value)
            {
                var matches = Regex.Matches(value, @"^[A-Za-z_-][A-Za-z0-9_-]*$");
                return (matches.Count > 0);
            }


    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Friday, April 7, 2017 7:16 AM
  • Try this too:

       bool is_valid = Regex.IsMatch( text, @"^[a-zA-Z0-9\-_]+$" );

    Sunday, April 9, 2017 8:06 AM