This Forum is ARCHIVE only
For LightSwitch questions,
please use the official forums at:

http://social.msdn.microsoft.com/forums/en-US/lightswitch/threads


A zip code validation rule I wrote in C#
Last Post 27 Feb 2012 02:38 AM by joiseystud. 1 Replies.
AddThis - Bookmarking and Sharing Button Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
joiseystudUser is Offline
New Member
New Member
Send Private Message
Posts:18
Avatar

--
27 Feb 2012 02:36 AM

    partial void PostalCode_Validate(EntityValidationResultsBuilder results)
    {
    // results.AddPropertyError("");

    //Add the dash if the user didn't enter it and the ZIP code is 9 characters
    if (this.PostalCode != null)
    {
    if (!this.PostalCode.Contains("-") & this.PostalCode.Length == 9)
    {
    this.PostalCode = this.PostalCode.Substring(0, 5) + "-" + this.PostalCode.Substring(5);
    }
    //Now validate based on regular expression pattern

    if (!Regex.IsMatch(this.PostalCode, @"^\d{5}$|^\d{5}-\d{4}$"))
    {
    results.AddPropertyError("Please enter a valid US ZIP code.");
    }
    }
    }
    joiseystudUser is Offline
    New Member
    New Member
    Send Private Message
    Posts:18
    Avatar

    --
    27 Feb 2012 02:38 AM

    I see no editing available so here is the corrected version

    partial void PostalCode_Validate(EntityValidationResultsBuilder results)
            {
                 results.AddPropertyError("");

                //Add the dash if the user didn't enter it and the ZIP code is 9 characters
                if (this.PostalCode != null)
                {
                      if (!this.PostalCode.Contains("-") & this.PostalCode.Length == 9)
                        {
                       this.PostalCode = this.PostalCode.Substring(0, 5) + "-" + this.PostalCode.Substring(5);
                        }
                //Now validate based on regular expression pattern

                      if (!Regex.IsMatch(this.PostalCode, @"^\d{5}$|^\d{5}-\d{4}$"))
                    {
                        results.AddPropertyError("Please enter a valid US ZIP code.");
                    }
                }
            }v

    You are not authorized to post a reply.


    Microsoft Visual Studio is a registered trademark of Microsoft Corporation / LightSwitch is a registered trademark of Microsoft Corporation