Blog Archives

Validate Credit Card Numbers with Regular Expressions

Expressions below account for IIN numbers on CC’s of most major US issuers which includes validating starting character. These should be used in addition to Luhn Algorithm mod10 check.

These expressions can be used on clientside and codebehind to give your end users notification of invalid entry without wasting resources on a failed/invalid post. See snippet below. Expressions should technically be cross-platform.

Note: although very similar to those found on regular-expressions.info, there are some small differences which do not account for the old standards in my examples since these cards are no longer in circulation.

(C#)

string vs = @"((^4)[0-9]{15}$)|";
            string mc = @"((^5[1-5])[0-9]{14}$)|";
            string ax = @"((^3[4|7])[0-9]{14})$|";
            string ds = @"(^6(011|5[0-9]{2})[0-9]{12}$)";

string expirationmonth = @"((^[0-9]$)|(^[0-9][0-2]$))";

string ordertotalamount = @"((^[0-9]{1,5}$)|(^[0-9]{1,5}\.[0-9]{1,2}$))"; //does not account for currency symbols

System.Text.StringBuilder sbexp = new System.Text.StringBuilder();
            for (int i = 0; i <= 20; i++) //create list of years from now+20. same range amazon uses for card
            {
                sbexp.Append("(^" + (DateTime.Now.Year + i).ToString() + "$)|");
                if (i < 20) //don't append or on last
                {
                    sbexp.Append((DateTime.Now.Year + i).ToString());
                }
            }

            ((RegularExpressionValidator)validator).ValidationExpression = sbexp.ToString();

((RegularExpressionValidator)validator).ValidationExpression = vs+mc+ax+ds;

References
Wikipedia (IIN numbers), http://en.wikipedia.org/wiki/Bank_card_number
Wikipedia (Luhn Algorithm), http://en.wikipedia.org/wiki/Luhn_algorithm
RegularExpressions.info, http://www.regular-expressions.info/creditcard.html

URL Encode in .Net

C#: (ASP .Net)

System.Web.HttpUtility.UrlEncode()

C#: (Client/Server Environment)

Uri.EscapeUriString()

References
MSDN, WebUtility Classhttp://msdn.microsoft.com/en-us/library/system.net.webutility.aspx
MSDN, Uri Classhttp://msdn.microsoft.com/en-us/library/system.uri.escapeuristring.aspx
MSDN blogs, http://blogs.msdn.com/b/yangxind/archive/2006/11/09/don-t-use-net-system-uri-unescapedatastring-in-url-decoding.aspx

Using jQuery and ScriptManager to update dynamic page elements

I recently came across a situation where an element on the page was not updating dynamically after post backs.

It was quick and easy to add an update panel and the necessary logic to use the built in asp .Net ajax callbacks.

I simply added an update panel around the code that needed to be updated, and an invisible button within the update panel to trigger the postback. I then called this button using javascript click() event of the button.

However, this caused some conflicts with some other pre-existing ajax code and modal dialogs, so I turned to another solution.

Rather than re-work these controls and methods which were already functioning correctly and extensively implemented, I added a small snippet of code on page load for the content pages which I wanted to trigger a force reload. This did the trick.

VB:


ScriptManager.RegisterStartupScript(Me, Me.GetType(), "loadminicart", "$('#minicart').load('" & _
strRootSiteURL & "minicartdata.aspx');", False)

C#:


ScriptManager.RegisterStartupScript(this, this.GetType(), "loadminicart", "$('#minicart').load('" + strRootSiteURL + "minicartdata.aspx');", false);

“minicartdata.aspx” is a dynamic page with no masterpage, header, footer or body HTML tags specified – only div’s and content elements.

“minicart” is the id of the div which minicartdata.aspx will be loaded into.

In my specific instance, there were only two pages which I needed to force the minicart content to reload. As a requirement, I needed the reload to occur after an operation on these pages, so since there were only two pages, it was easy to specify this code in both.

In some cases, you may want to be able to easily trigger a refresh from code behind without having to place the above code in every page. For this I recommend simply creating a global function and placing the snippet above, then calling the global function from anywhere needed.

Another, more object oriented approach is to create your own type which inherits page, and use this as the base type for all your content pages. You could then place a function in your new type which contains the snippet above. This effectively has the same end result as using a global function.

Hope this information can be as useful to someone else as I found it to be. ;)

Follow

Get every new post delivered to your Inbox.