Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have used the following code but it is returning false though it should return true

string check,zipcode;
zipcode="10001 New York, NY";
check=isalphanumeric(zipcode)

public static Boolean isAlphaNumeric(string strToCheck)
{
    Regex rg = new Regex("[^a-zA-Z0-9]");

    //if has non AlpahNumeric char, return false, else return true.
    return rg.IsMatch(strToCheck) == true ? false : true;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
741 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try this one:

public static Boolean isAlphaNumeric(string strToCheck)
{
    Regex rg = new Regex(@"^[a-zA-Z0-9s,]*$");
    return rg.IsMatch(strToCheck);
}

It's more undestandable, if you specify in regex, what your string SHOULD contain, and not what it MUST NOT.

In the example above:

  • ^ - means start of the string
  • []* - could contain any number of characters between brackets
  • a-zA-Z0-9 - any alphanumeric characters
  • s - any space characters (space/tab/etc.)
  • , - commas
  • $ - end of the string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...