How to detect Mobile Device Browsers?

We can easily detect the Mobile device browsers using the following C# code.
  1. public bool IsMobileDevice(string UserAgent)
  2. {
  3.     bool result = false;
  4.     Regex b = new Regex(@"(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino", RegexOptions.IgnoreCase | RegexOptions.Multiline);
  5.     Regex v = new Regex(@"1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-", RegexOptions.IgnoreCase | RegexOptions.Multiline);
  6.     if ((b.IsMatch(userAgent) || v.IsMatch(userAgent.Substring(0, 4))))
  7.     {
  8.         result = true;
  9.     }
  10.     return result;
  11. }

  1. string userAgent = Context.Request.ServerVariables["HTTP_USER_AGENT"];
  2. var IsMobile = IsMobileDevice(userAgent);
Monday, 30 December 2013
Posted by Jebastin
Tag :

How to hide the first option of a select using jQuery

When using "jquery.selectbox.js" plugin the "select & option" tags will be converted into "ul li" at run time. In this case it's hard to hide the first option using the normal select option script. The following solution rectifies this situation.

HTML:

  1. <div class="language">
  2. <select id="language" name="language" sb="76231018" style="display: none;">
  3. <option value="0">Select Language</option>
  4. <option value="http://www.example.com/en/" page="master">English</option>
  5. <option value="http://www.example.com/fr/" page="master">French</option>
  6. <option value="http://www.example.com/es/" page="master">Spanish</option>
  7. </select>
  8. <div id="sbHolder_76231018" class="sbHolder" tabindex="0">
  9.  <a id="sbToggle_76231018" href="#" class="sbToggle"></a>
  10.  <a id="sbSelector_76231018" href="#" class="sbSelector">Select Language</a>
  11.  <ul id="sbOptions_76231018" class="sbOptions" style="display: none;">
  12.    <li><a href="#0" rel="0" class="sbFocus">Select Language</a></li>
  13.    <li><a href="http://www.example.com/en/" rel="http://preview.smp.com/en/">English</a></li>
  14.    <li><a href="http://www.example.com/fr/" rel="http://preview.smp.com/fr/">French</a></li>
  15.    <li><a href="http://www.example.com/es/" rel="http://preview.smp.com/es/">Spanish</a></li>
  16.  </ul>
  17. </div>
  18. </div>

jQuery:

  1. <script type="text/javascript">
  2.     $(document).ready(function () {
  3.         $(".language .sbHolder").click(function () {
  4.             $(".language .sbHolder ul.sbOptions li:first").hide();
  5.         });
  6.     });
  7. </script>
Friday, 20 December 2013
Posted by Jebastin
Tag : ,

Find the Distance between 2 Latitude Longitude Co-Ordinates using Haversine formula

The following function is used to find the distance between two Latitude & Longitude co-ordinates using Haversine formula.

The haversine formula is an equation important in navigation, giving great-circle distances between two points on a sphere from their longitudes and latitudes. It is a special case of a more general formula in spherical trigonometry, the law of haversines, relating the sides and angles of spherical triangles.

  1. public double HaversineDistance(LatLng pos1, LatLng pos2, DistanceUnit unit)
  2. {
  3.     double R = (unit == DistanceUnit.Miles) ? 3959 : 6371;
  4.     var la1 = pos1.Latitude;
  5.     var lo1 = pos1.Longitude;
  6.     var la2 = pos2.Latitude;
  7.     var lo2 = pos2.Longitude;
  8.     var lat1 = la1 * Math.PI / 180;
  9.     var lon1 = lo1 * Math.PI / 180;
  10.     var lat2 = la2 * Math.PI / 180;
  11.     var lon2 = lo2 * Math.PI / 180;
  12.     var dLat = lat2 - lat1;
  13.     var dLon = lon2 - lon1;
  14.     var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
  15.     var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
  16.     var d = R * c;
  17.     return d;
  18. }
  1. public class LatLng
  2. {
  3.     public double Latitude { get; set; }
  4.     public double Longitude { get; set; }
  5.     public LatLng()
  6.     {
  7.     }
  8.     public LatLng(double lat, double lng)
  9.     {
  10.         this.Latitude = lat;
  11.         this.Longitude = lng;
  12.     }
  13. }
Posted by Jebastin
Tag : ,

Microsoft SQL Server Function to Get Ordinal Suffix of a Number

This AddOrdinal function will be useful when we needed to display the date format as 1st July -3rd October. There is nothing in there about ordinals, but it can't be done using String.Format. However it's not really that hard to write a function to do it. The following Microsoft SQL Server Function is used to get the Ordinal Suffix of any number.
  1. CREATE FUNCTION [Internal].[GetNumberAsOrdinalString]
  2. (
  3.     @num int
  4. )
  5. RETURNS nvarchar(max)
  6. AS
  7. BEGIN
  8.     DECLARE @Suffix nvarchar(2);
  9.     DECLARE @Ones int; 
  10.     DECLARE @Tens int;
  11.     SET @Ones = @num % 10;
  12.     SET @Tens = FLOOR(@num / 10) % 10;
  13.     IF @Tens = 1
  14.     BEGIN
  15.         SET @Suffix = 'th';
  16.     END
  17.     ELSE
  18.     BEGIN
  19.     SET @Suffix =
  20.         CASE @Ones
  21.             WHEN 1 THEN 'st'
  22.             WHEN 2 THEN 'nd'
  23.             WHEN 3 THEN 'rd'
  24.             ELSE 'th'
  25.         END
  26.     END
  27.     RETURN CONVERT(nvarchar(max), @num) + @Suffix;
  28. END

 Related Function in other languages:

C# Code to Get Ordinal Suffix of a Number

PHP Code to Get Ordinal Suffix of a Number

Thursday, 19 December 2013
Posted by Jebastin
Tag :

PHP Code to Get Ordinal Suffix of a Number

It's a user-defined function which is a lot simpler than you think. Though there might be a .NET function already in existence for this, the following function (written in PHP) does the job. It shouldn't be too hard to port it over. This ordinal function will be useful when we needed to display the date format as 1st December 2013.
  1. function ordinal($num) {
  2.     $ones = $num % 10;
  3.     $tens = floor($num / 10) % 10;
  4.     if ($tens == 1) {
  5.         $suff = "th";
  6.     } else {
  7.         switch ($ones) {
  8.             case 1 : $suff = "st"; break;
  9.             case 2 : $suff = "nd"; break;
  10.             case 3 : $suff = "rd"; break;
  11.             default : $suff = "th";
  12.         }
  13.     }
  14.     return $num . $suff;
  15. }

 Related Function in other languages:

C# Code to Get Ordinal Suffix of a Number

Microsoft SQL Server Function to Get Ordinal Suffix of a Number

Posted by Jebastin
Tag :

C# Code to Get the Latitude and Longitude Co-Ordinates of a Place using Address or Zip-code

The following C# Code is used to get the Latitude and Longitude Co-Ordinates of a particular place anywhere in the World using the Address or Zip-code. To get the Latitude and Longitude Co-Ordinates using Address, call the function like this, getLatLong("Briarwood, NY 11435-2694",""). To get the Latitude and Longitude Co-Ordinates using the Zip-code, call the function like this, getLatLong("","11435").
  1. public string getLatLong(string Address, string Zip)
  2. {
  3.     string latlong = "", address = "";
  4.     if (Address!=string.Empty)
  5.     {
  6.         address = "http://maps.googleapis.com/maps/api/geocode/xml?address=" + Address + "&sensor=false";
  7.     }
  8.     else
  9.     {
  10.         address = "http://maps.googleapis.com/maps/api/geocode/xml?components=postal_code:" + Zip.Trim() + "&sensor=false";
  11.     }
  12.     var result = new System.Net.WebClient().DownloadString(address);
  13.     XmlDocument doc = new XmlDocument();
  14.     doc.LoadXml(result);
  15.     XmlNodeList parentNode = doc.GetElementsByTagName("location");
  16.     var lat = "";
  17.     var lng = "";
  18.     foreach (XmlNode childrenNode in parentNode)
  19.     {
  20.         lat = childrenNode.SelectSingleNode("lat").InnerText;
  21.         lng = childrenNode.SelectSingleNode("lng").InnerText;
  22.     }
  23.     latlong = Convert.ToString(lat) + "," + Convert.ToString(lng);
  24.     return latlong;
  25. }
Posted by Jebastin
Tag : ,

C# Code to Get Ordinal Suffix of a Number

There is no inbuilt capability in the .NET Base Class Library to get the Ordinals. This AddOrdinal function will be useful when we needed to display the date format as 1st July -3rd October. There is nothing in there about ordinals, but it can't be done using String.Format. However it's not really that hard to write a function to do it. The following C# code is used to get the Ordinal Suffix of any number.
  1. public static string AddOrdinal(int num)
  2. {
  3.     if (num <= 0) return num.ToString();
  4.     switch (num % 100)
  5.     {
  6.         case 11:
  7.         case 12:
  8.         case 13:
  9.             return num + "th";
  10.     }
  11.     switch (num % 10)
  12.     {
  13.         case 1:
  14.             return num + "st";
  15.         case 2:
  16.             return num + "nd";
  17.         case 3:
  18.             return num + "rd";
  19.         default:
  20.             return num + "th";
  21.     }
  22. }

Another way:

  1. public static string[] SuffixLookup = { "th","st","nd","rd","th","th","th","th","th","th" };
  2.  
  3. public static string AppendOrdinalSuffix(int number)
  4. {
  5. if (number % 100 >= 11 && number % 100 <= 13)
  6. {
  7. return number + "th";
  8. }
  9. return number + SuffixLookup[number% 10];

And the more simplest way:

  1. private static string GetOrdinalSuffix(int num)
  2. {
  3.     if (num.ToString().EndsWith("11")) return "th";
  4.     if (num.ToString().EndsWith("12")) return "th";
  5.     if (num.ToString().EndsWith("13")) return "th";
  6.     if (num.ToString().EndsWith("1")) return "st";
  7.     if (num.ToString().EndsWith("2")) return "nd";
  8.     if (num.ToString().EndsWith("3")) return "rd";
  9.     return "th";

 Related Function in other languages:

PHP Code to Get Ordinal Suffix of a Number

Microsoft SQL Server Function to Get Ordinal Suffix of a Number

Posted by Jebastin
Tag : ,

Link To This Post/Page

Spread The Word

Add this button to your blog:
JJ Technology Solutions

Blog Archive

Trackers

eXTReMe Tracker
facebook

- Copyright © JJ Technology Solutions - Powered by Source Code Solutions -