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;

}




Tags: , , , , , ,

One Response to “Validation of web site address on server side using c-sharp and Regular expression”

  1. Eryn Benz says:

    I was just searching for this info for a while. After 6 hours of continuous Googleing, at last I got it in your site. I wonder what’s the lack of Google strategy that do not rank this type of informative websites in top of the list. Generally the top websites are full of garbage.