Monday, April 1, 2013

How to convert string to TitleCase



public static String TitleCaseString(String s)
{
   if (s == null) return s;
   String[] words = s.Split(' ');
   for (int i = 0; i < words.Length; i++)
   {
     if (words[i].Length == 0) continue;
     Char firstChar = Char.ToUpper(words[i][0]);
     String rest = "";
     if (words[i].Length > 1)
     {
      rest = words[i].Substring(1).ToLower();
     }
     words[i] = firstChar + rest;
   }
    return String.Join(" ", words);
}

How to convert string to SentenceCase



public static string SentenceCase(string text)
{
     string temporary = text.ToLower();
     string result = "";
     while (temporary.Length > 0)
     {
        string[] splitTemporary = SplitAtFirstSentence(temporary);
        temporary = splitTemporary[1];
        if (splitTemporary[0].Length > 0)
        {
           result += CapitaliseSentence(splitTemporary[0]);
        }
        else
        {
           result += CapitaliseSentence(splitTemporary[1]);
           temporary = "";
        }
     }
  return result;
}

private static string CapitaliseSentence(string sentence)
{
    string result = "";
    while (sentence[0] == ' ')
    {
     sentence = sentence.Remove(0, 1);
     result += " ";
    }
    if (sentence.Length > 0)
    {
     result += sentence.TrimStart().Substring(0, 1).ToUpper();
     result += sentence.TrimStart().Substring(1,sentence.TrimStart().Length - 1);
    }
   return result;
}

private static string[] SplitAtFirstSentence(string text)
{
  int lastChar = text.IndexOfAny(new char[] { '.', ':', '\n', '\r', '!', '?' }) + 1;
  if (lastChar == 1)
  {
    lastChar = 0;
  }
   return new string[] { text.Substring(0, lastChar),text.Substring(lastChar,   text.Length - lastChar) };
}

Saturday, March 30, 2013

How to create place holder using image with Css

<div>
<input type='text' name='Email' id='Email' class="email-wmark" />
</div>

<div>
<input type='password' name='Password' id='Email' class="pass-wmark" />
</div>


$(function () {
$('#Email').focus(function () {
            $(this).removeClass('email-wmark');
       });

       $('#Email').blur(function () {
            if ($(this).val() == '') {
                $(this).toggleClass('email-wmark');
              };
        });

        $('#Password').focus(function () {
            $(this).removeClass('pass-wmark');
        });

        $('#Password').blur(function () {
            if ($(this).val() == '') {
                $(this).toggleClass(' pass-wmark');
            };
        });
    });


.email-wmark{background:#fff url(email-wmark.png) no-repeat 5px center !important;}
.pass-wmark{background:#fff url(pass-wmark.png) no-repeat 5px center !important;}
 

How to remove alphabetic characters from a string


string a = "(570) 847—1059 Alternate";
string b = "570 847 1059 Alternate";
string c = "570-847—1059 Alternate";

a = Regex.Replace(a, "[A-Za-z]", "");
b = Regex.Replace(b, "[A-Za-z]", "");
c = Regex.Replace(c, "[A-Za-z]", "");

Thursday, March 21, 2013

How to Check checkbox and Radio button when click Names


How to Check checkbox and Radio button when click Names




How to check checkbox when click names:

<input type="checkbox" value='flight' name="chkCommon" id="ChkFlight" />
<label for="ChkFlight">Flight</label>

How to check Radiobutton when click names:

<span class="chk-box-round">
@Html.RadioButton("TripType", "ROUND", new { id = "round_trip", @checked = true})
<label for="round_trip">Round Trip</label>
</span>

Thursday, March 7, 2013

How to convert Viewbag Value as Array Using Javascript in MVC

function GetMonth(monthStart) {
   if (monthStart == 1) {
       $('#ddlExpirationMonth').empty();
       $('#ddlExpirationMonth').append('<option value="0">MM</option>');
     
       var array = @Html.Raw(Json.Encode(@ViewBag.CreditCardMonth));
      
       for(var i = 0; i < array.length; i++) {
       $('#ddlExpirationMonth').append('<option value="' + array[i].Id
+  '">' + array[i].Name + '</option>');
  }
     }
      else {
     for (var j = 1; j < monthStart; j++) {
       if (j < 10)
       j = "0" + j;
       $("#ddlExpirationMonth option[value='" + j + "']").remove();
}
}
}