Validation of web site address on server side using c-sharp and Regular expression

Validating web site address at server side in asp.net using c-sharp.

A detail web site address  validation method where Regular expression is being used for a baisc web addrss validation, then web address is being splitted by dot and forward slash to check if it contains http: and if its defined more than once in the web address return false as web addrss is not a vaild web address.

Then I am checking for the dot in web site address if there is no dot in the web site address then its an invalid web address. Next I am checking of first word is http or https if not then its an invalid web address, also there must be atleast two dots in the web address at last we need to check at end there must be at least 2 chracters.

public static bool ValidateWebSiteAddress(string webAdress)
{
bool isValid = true;
Regex weburl = new Regex(@”http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?”);
char[] sepCharacters ={ ‘.’, ‘/’ };
int iCounthttp = 0;
string[] words = webAdress.ToLower().Split(sepCharacters);

foreach (string word in words)
if (word.Equals(“http:”))
iCounthttp++;

if (iCounthttp > 1)
isValid = false;

//check against regular expression
if (!weburl.IsMatch(webAdress))
isValid = false;

//check dots are present
if (!webAdress.Contains(“.”))
isValid = false;
//check for http and https
if (words[0].Length > 6 || words[0].Length < 5)
isValid = false;
//check if there are 2 dots in the adress
if (CharCount(webAdress, “.”) < 2)
isValid = false;
if (words.Length <= 5)
{
if (String.IsNullOrEmpty(words[words.GetUpperBound(0)]))
isValid = false;
if (words[words.GetUpperBound(0)].Length < 2)
isValid = false;
}

return isValid;

}

Remove extra spaces from string using regular expression, asp.net, c#

Removes extra space from the string using Regular expression.

public static readonly RegexOptions Options =          RegexOptions.IgnorePatternWhitespace |
RegexOptions.Singleline;
public string RemoveExtaSpaces(string text)
{
Regex regex = new Regex(@”\s{2,}”, Options);
text = regex.Replace(text.Trim(), ” “); //This line removes extra spaces and make space exactly one.
//To remove the  space between the end of a word and a punctuation mark used in the text we will
//be using following line of code

regex=new Regex(@”\s(\!|\.|\?|\;|\,|\:)”); // “\s” whill check for space near all puntuation marks in side ( \!|\.|\?|\;|\,|\:)”); )
text = regex.Replace(text, “$1″);
return text;

}

Above function removes all extra spaces in the string to make one space exactly. Feel free to use, its been used and tested

static void Main(string[] args)

{

string mytext = “blah    blah    blah   blah   .”;

Console.WriteLine(mytext);

Console.WriteLine(RemoveExtaSpaces(mytext));

}

Result

blah blah blah blah.