Sunday, 9 February 2014

Regular Expression for Indian Mobile Numbers.

Recently I was developing website using ASP.NET MVC 4 and wanted to add validation to my form to validate Indian mobile number. I wanted it to validate numbers entered in following format as valid!

1. It can be 10 digit number with first digit should be either 7,8 or 9
2. It can precede with a digit 0 or +91 or 0091. This is optional.

Below is sample code snippet showing the regular expression used to validate Indian Mobile Number.

         // Below is sample code snippet.
            function bool IsValidMobileNumber(string mobileNumber){
           string pattern = @"^((0091)|(\+91)|0?)[789]{1}\d{9}$";
            Regex regMobile = new Regex(pattern);
            return regMobile.IsMatch(mobileNumber);
           }


You can decorate the Model value as below when using it in ASP,NET MVC,
//This is required to annotate the public property of model.
using System,ComponentModel.DataAnnotations;


public class    SomeModel {

  [RegularExpression(@"^((0091)|(\+91)|0?)[789]{1}\d{9}$", ErrorMessage = "Mobile Number is not valid!")]
        public string MobileNumber get;set;


}
Here is an interpretation of this Regular Expression.

  • ^ and $ are anchor tags and indicate start and end of expression to be matched.
  • We want three groups to be used either 0091,+91 or 0  and hence the group are specified in () brackets with '|' character to indicate or logic  '?' indicate it is optional.
  • I wanted 7,8 or 9  as first digit of 10 digit mobile number so [789]{1} indicate only valid digits in rectangular bracket and curly braces indicate I want it to be exact one digit long.
  • Please note '+' has special meaning in regular expression and need to be preceded with '\' character if it is to be used as normal character and hence (\+91) 
  • '\d{9}' indicate allow only valid digits 0 to 9 and should be 9 digits long.
I have tested it with different valid combinations and it works fine.
e.g. of valid numbers are given below.
  •   00919123456789
  • +0917123456789
  • 09123456789
  • 7123456780
  • 8012345678
  • 9012345678

I hope you will find this useful!





Friday, 27 December 2013

Applying Observer Design pattern using C#

Design patterns are solutions to commonly recurring problems incurred during software development. Observer is one of the most popular design problem and is also known as publisher/subscriber pattern. If you want to see how this pattern can be applied in real life scenario then please read my article.
Real Time Stock Feed Dashboard (Applying Observer Design pattern using WPF in c# )


Tuesday, 17 December 2013

Office Development Using VSTO and C#

If you are interested in doing Microsoft Office Development and would like to leverage the power of latest .NET framework features such as Entity Framework and C# language features such as LINQ and Lambda Expressions then the below link to my article may be of interest to you. I request you read this article and let me know if you need any further help or have any queries. Enjoy reading!
Implementing Custom Action Pane and Custom Ribbon For Excel 2010 using VSTO and C#

Thursday, 24 January 2013

Can't create Database. Permission denied on master database.

Recently I was working  on ASP.NET MVC 4.0 Project using Visual studio 2012 and trying entity framework code first. Entity framework Code First approach automatically creates the  database schema and tables based on model defined in the code.
After doing build and running my sample project I was getting an exception with the message.
"Can't create Database. Permission denied on master database!". 
I was surprised as I was administrator on my laptop and it took me some time to figure out what's happening. Finally I manage to resolve this issue.  Here is the detailed explanation
1.With Visual studio 2012 SQL-Server 2012 LocalDB Express is used for development.
2.On my laptop I had one more instance of SQL-Server express.  Entity framework was looking for this instance and I didn't  have sysadmin rights on this instance and hence was giving the issue. Also I had forgotten password for sa user and couldn't give rights to my account.
3. So I stopped this instance and disabled this service. So that I can use LocalDB express.
4. Last thing was telling the entity framework to check for LocalDB and I found the setting to be done in web.config. The setting is given below.

.

<entityFramework>
    <defaultConnectionFactory
        type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, 
            EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
5. I was then ready to recompile and test and it worked fine.
I hope you will find this useful! as I spent couple of hours to investigate what's happening.






Sunday, 14 October 2012

Unable to Create ActivexX control error message on 64 bit machines

Recently I was migrating one of the VBS script to 64 bit environment for one of our client. It was configured as scheduled task using Windows Task Scheduler. I noticed it stopped working. So upon investigation I found that the script was giving  "Can't create ActiveX control component" message.
Since the script was using 32 bit version of DLL (CSScript.DLL)  and since runtime was 64 bit it was giving above message. There are two ways to resolve this issue.

Run the Script Manually:

1. To run the script manually you need to run it from 32 bit version of command prompt (cmd.exe)  This is under  Windows\SYSWOW64\ folder.
2. Create short cut of this version of cmd.exe and keep it on your desktop
3. Run this shortcut to go to command line window and run your VBS script and it should work without any errors,

Schedule for Auto running of VBS script:
1. You can use windows task scheduler for configure this as scheduled task.
2.  Create the task and run the VBS script using WSSCRIPT.exe utility (32 bit version). This is again available under Windows\SYSWOW64\ folder.
3. Pass the name of the vbs script  as command line parameter.
4. You are ready to schedule and run it as scheduled task.


I hope you all will find this information useful.



Sunday, 15 April 2012

Task Tracker Offline Web Application for Mobile.

Please Read my article on above topic in code project using link below.
Task Tracker Offline Web Application using HTML5,JQuery
I hope you will find it interesting as I explored features of HTML5, JQUERY,JQUERY-UI,JQuery Validation and Knockout.js.