Tuesday, March 14, 2017

Validating date format using java script


 // Validates that the input string is a valid date formatted as "mm/dd/yyyy"  
 function isValidDate(dateString)  
 {  
   // First check for the pattern  
   if(!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(dateString))  
     return false;  
   // Parse the date parts to integers  
   var parts = dateString.split("/");  
   var day = parseInt(parts[1], 10);  
   var month = parseInt(parts[0], 10);  
   var year = parseInt(parts[2], 10);  
   // Check the ranges of month and year  
   if(year < 1000 || year > 3000 || month == 0 || month > 12)  
     return false;  
   var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];  
   // Adjust for leap years  
   if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))  
     monthLength[1] = 29;  
   // Check the range of the day  
   return day > 0 && day <= monthLength[month - 1];  
 };  

Thursday, January 26, 2017

Simple encryption and decryption using key value in C#

Encryption

 public string Encrypt(string plainText)  
     {  
       try  
       {  
         string key = "abcd123xyz" + DateTime.Now.Year.ToString() + "#";  
         byte[] EncryptKey = { };  
         byte[] IV = { 55, 34, 87, 64, 87, 195, 54, 21 };  
         EncryptKey = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, 8));  
         DESCryptoServiceProvider des = new DESCryptoServiceProvider();  
         byte[] inputByte = Encoding.UTF8.GetBytes(plainText);  
         MemoryStream mStream = new MemoryStream();  
         CryptoStream cStream = new CryptoStream(mStream, des.CreateEncryptor(EncryptKey, IV), CryptoStreamMode.Write);  
         cStream.Write(inputByte, 0, inputByte.Length);  
         cStream.FlushFinalBlock();  
         return Convert.ToBase64String(mStream.ToArray());  
       }  
       catch (Exception)  
       {  
         return "0";  
       }  
     }  

Decryption

 public string Decrypt(string encryptedText)  
     {  
       try  
       {  
         string key = "abcd123xyz" + DateTime.Now.Year.ToString() + "#";           byte[] DecryptKey = { };  
         byte[] IV = { 55, 34, 87, 64, 87, 195, 54, 21 };  
         byte[] inputByte = new byte[encryptedText.Length];  
         DecryptKey = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, 8));  
         DESCryptoServiceProvider des = new DESCryptoServiceProvider();  
         inputByte = Convert.FromBase64String(encryptedText);  
         MemoryStream ms = new MemoryStream();  
         CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(DecryptKey, IV), CryptoStreamMode.Write);  
         cs.Write(inputByte, 0, inputByte.Length);  
         cs.FlushFinalBlock();  
         System.Text.Encoding encoding = System.Text.Encoding.UTF8;  
         return encoding.GetString(ms.ToArray());  
       }  
       catch (Exception)  
       {  
         return "0";  
       }  
     }  


Wednesday, January 25, 2017

Area registration OR Area routing in MVC C#

Sample
 public override void RegisterArea(AreaRegistrationContext context)   
     {  
       context.MapRoute(  
         "Cpanel_default",  
         "Cpanel/{controller}/{action}/{id}",  
         new { controller="Home", action = "Index", id = UrlParameter.Optional },  
         new [] {"NT.Areas.Cpanel.Controllers"}  
       );  
     }  

Manage css dynamically in razor view based on condition - MVC

Sample
  <div class="col-lg-4 col-md-3 @(app.AppPath=="#"?"disableDiv":"")">  
 Your data  
 </div>