次の方法で共有


Common Validation Rules in LightSwitch Business Applications

Checking the validity of data input is a common requirement for any application that interacts with humans (and other systems), particularly business applications. I’ve never seen or written a data-entry application without implementing common validation rules for inputting data. LightSwitch has many ways to implement validation declaratively through the data designer as well as supporting custom code you write. Field (or property) validation is just one aspect of writing business rules in LightSwitch, but it’s definitely an important one. It’s your “first line of defense” in keeping user entered data consistent.

Although LightSwitch has some built-in business types and declarative validation settings, many times we need to write small snippets of code to check the format of data entered on a screen. Common things in the USA are States, ZIP codes, Social Security Numbers, UPIN, ISBN, etc. But you may also want to prevent things like numeric and symbolic values in people’s or place’s names or a variety of other rules based on string manipulation.

In this post, I’ll show you how to define declarative rules as well as write custom validation code in LightSwitch. I’ll also show you some common patterns you can use to validate strings. Let’s get started!

Declarative Validation Rules

First let’s walk through the types of validation you can specify declaratively without having to write any code. Let’s take a customer entity that we’ve designed with the data designer. It has the following fields:

image

Required Fields & Business Types

The first step to validating these fields is to determine their types and which ones are required. LightSwitch will automatically handle required fields as well as validation that comes with business types so you can set these up declaratively using the data designer without having to write any code. The built-in business types are Email, Phone, Money and Image but this is an extensibility point so you can download more. On our customer entity notice I’m only requiring that LastName is filled out. Required fields prevent the user from saving data if the field is left blank and the labels show up bolded on the screen.

image

When any validation fails, a message is displayed when the user’s cursor is in the field or if they click the validation summary at the top of the screen.

image

Notice that I have also selected the “Phone Number” and “Email Address” business types for the Phone and Email fields on our customer entity. Business types come with built-in validation and have additional properties that you can set in the properties window to indicate how the validation should work. For Email Address, you can select whether you want to provide a default email domain and if the domain is required:

image

For Phone Number you can supply additional formats it should validate against and in what order:

image

Specifying Field Maximum Lengths

Another important declarative validation rule is specifying a maximum length. By default all string fields default to 255 characters. This works for most of our fields because they have variable lengths and 255 will be plenty of room for our data. However, the SSN, State, and ZIP fields are fixed lengths of 11, 2 and 10 characters respectively. Anything over that isn’t a valid piece of data. You specify maximum lengths in the properties window at the bottom in the Validation section:

image

Preventing Duplicates with a Unique Index

You can also prevent duplicates by including required fields in a unique index. For instance if I wanted to prevent duplicate Last Names in the system then I could include it in the unique index.

image

Note that this adds the field to a unique index for the table and is enforced by the database. So the validation message will appear after the data is saved and checked on the server side. In the case of my customer entity this is not a good idea to limit duplicates this way because it’s pretty common to have customers with the same last name. You might consider using SSN but then we’d have to make this a required field and only customers with an SSN would be allowed into the system. This is too restrictive in this case, but including fields in a unique index can work well for other types of data entities where you want to prevent duplicate records.

Custom Validation Rules

When you can’t express a validation rule declaratively then you need to write some code. To write custom validation code, select the property you want on the entity then drop down the “Write Code” button and select the property_ Validate method:

image

This will open the code editor to a method stub that allows you to write the custom validation code. When a rule fails, you use the results object to add your error message.

 Private Sub LastName_Validate(results As EntityValidationResultsBuilder)
    ' Check the rule, if it fails then display an error message on the field:
    results.AddPropertyError("<Error Message>")
End Sub

You can also specify warnings and informational messages as well. Only error messages prevent data from being saved. To specify a warning or informational message on a field use the AddPropertyResult method and pass it the level of severity:

image

You can also specify entity-level errors which aren’t specific to certain fields, instead they will only show up in the validation summary at the top of the screen. You use the AddEntityError and AddEntityResult methods for this. These property validation methods run on the client first and then again on the server, however you just write them once and LightSwitch will take care of calling them at the appropriate times. For a deeper understanding of the validation framework please read: Overview of Data Validation in LightSwitch Applications

Simple String Validation & Formatting

The first rule I want to enforce is that the State field should always be entered in uppercase. There are a lot of string methods you can use to manipulate and validate string data. For instance to format strings in a variety of ways you can use the String.Format() method. There are also methods to find characters at certain positions (IndexOf), to check if a string contains another string (Contains), to return parts of strings (Substring), to trim whitespace (Trim), and much much more. See the documentation for all the string methods available to you.

In order to format the State field we can simply use the ToUpper() method. Since State isn’t a required field, we first check to see if the State property has a value and if so we make it upper case.

 Private Sub State_Validate(results As EntityValidationResultsBuilder)
    If Me.State <> "" Then
        Me.State = Me.State.ToUpper
    End If
End Sub

Notice that this doesn’t report a validation error to the user, it just formats the string they enter. You can also use the property_ Validate methods to perform formatting as well because the _Validate method will fire when the user tabs out of the field when the method runs on the client. However we still need to validate whether they entered a valid State code – “AA” is currently allowed and this isn’t a valid U.S. State. In order to validate all the possible state code combinations we can use Regular Expressions.

Using Regular Expressions

Regular expressions are used by many text editors, utilities, and programming languages to search and manipulate text based on patterns. Regular expressions are used all over the web to validate user input. In fact, one of my favorite sites is www.RegExLib.com which has thousands of community submitted patterns you can use in your own validation rules. In order to use regular expressions in your _Validate methods you use the RegEx class located in the System.Text.RegularExpressions namespave. So in order to check that the State is a valid US State we can write the following in bold:

 Imports System.Text.RegularExpressions

Namespace LightSwitchApplication
    Public Class Customer

        Private Sub State_Validate(results As EntityValidationResultsBuilder)
            If Me.State <> "" Then
                Me.State = Me.State.ToUpper


                Dim pattern = "^(?:(A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|" +                              "LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|P[AR]|RI|S[CD]|" +                              "T[NX]|UT|V[AIT]|W[AIVY]))$"
                                If Not Regex.IsMatch(Me.State, pattern) Then                    results.AddPropertyError("Please enter a valid US State.")                End If

End If
End Sub
End Class
End Namespace

Similarly we can use regular expressions to check ZIP codes as well. However I want to allow both 5 digit and 9 digit ZIP codes. I also want to allow the user to not have to specify the dash so we’ll do a little formatting as well first.

 Private Sub ZIP_Validate(results As EntityValidationResultsBuilder)
    If Me.ZIP <> "" Then
        'Add the dash if the user didn't enter it and the ZIP code is 9 characters
        If Not Me.ZIP.Contains("-") AndAlso Me.ZIP.Length = 9 Then
            Me.ZIP = Me.ZIP.Substring(0, 5) + "-" + Me.ZIP.Substring(5)
        End If
        'Now validate based on regular expression pattern
        If Not Regex.IsMatch(Me.ZIP, "^\d{5}$|^\d{5}-\d{4}$") Then
             results.AddPropertyError("Please enter a valid US ZIP code.")
        End If
    End If
End Sub

Another rule I want to enforce is the SSN format that has the pattern “3 digits (dash) 2 digits (dash) 4 digits”. I want to do the same type of thing we did above where we won’t require the user to enter the dashes. So we can write the following:

 Private Sub SSN_Validate(results As EntityValidationResultsBuilder)
    If Me.SSN <> "" Then
        'Add the dashes if the user didn't enter it and the SSN is 9 characters
        If Not Me.SSN.Contains("-") AndAlso Me.SSN.Length = 9 Then
            Me.SSN = Me.SSN.Substring(0, 3) + "-" + Me.SSN.Substring(3, 2) + "-" + Me.SSN.Substring(5)
        End If

        'Now validate based on regular expression pattern
        If Not Regex.IsMatch(Me.SSN, "^\d{3}-\d{2}-\d{4}$") Then
            results.AddPropertyError("Please enter a valid SSN (i.e. 123-45-6789).")
        End If
    End If
End Sub

You can do a lot with regular expressions and string manipulation. The last rule I want to enforce is not allowing users to enter numbers or symbols in the LastName and FirstName fields. They should only contain alphabetical characters and spaces are allowed. We can do something like this to enforce that:

 Private Sub LastName_Validate(results As EntityValidationResultsBuilder)
    If Me.LastName <> "" Then
        'This pattern only allows letters and spaces
        If Not Regex.IsMatch(Me.LastName, "^[a-zA-Z\s]+$") Then
            results.AddPropertyError("Last Name can only contain alphabetical characters.")
        End If
    End If
End Sub

Private Sub FirstName_Validate(results As EntityValidationResultsBuilder)
    If Me.FirstName <> "" Then
        'This pattern only allows letters and spaces
        If Not Regex.IsMatch(Me.LastName, "^[a-zA-Z\s]+$") Then
            results.AddPropertyError("First Name can only contain alphabetical characters.")
        End If
    End If
End Sub

Notice that in this last example I’m using the same pattern. You’ll most likely have fields across entities in your application where you’ll want to use the same validation checks. When you start copying and duplicating code you should stop and think about consolidating it into a single library or class that you can call. This way if you have a bug in your validation code you fix it in just one place. Remember, the less code you write the less bugs you’ll have. ;-)

Creating a Common Validation Module

Let’s create a validation module that we can call from our validation methods that encapsulates all the types of validation routines we’d want to support across all the entities in our application. In the Solution Explorer flip to “File View” and under the Common project expand the UserCode folder. Right-click and add a new Module. I’ll name it CommonValidation.

image

To make these validation rules easy to discover and call from our _Validate methods we’ll create them as Extension Methods. Basically extensions methods “extend” types, like a string or integer or any other object type with your own custom methods. What’s cool is they appear in IntelliSense with you type the dot “.” after the type. To place extension methods in our module we just need to import the  System.Runtime.CompilerServices and then attribute our method with the <Extension()> attribute.

Let’s take a simple example before we move our complex validation code in here. For instance let’s create an extension method that extends a string type with a method called “MyMethod”. The first parameter to your extension method is the type you are extending. Here’s how we could write the module. (Notice I added the comment section by typing three single quotes (‘’’) above the <Extension>):

 Imports System.Runtime.CompilerServices

Module CommonValidation
    ''' <summary>
    ''' This is my extension method that does nothing at the moment. ;-)
    ''' </summary>
    ''' <param name="value">extend the string type</param>
    ''' <remarks></remarks>
    <Extension()>
    Public Sub MyMethod(ByVal value As String)
        'Do something
    End Sub
End Module

If we flip back to one of the property_ Validate methods then we will now see this extension method in IntelliSense on any string type. (You need to flip to the “All” tab).

image

Notice there are a lot of “built-in” extension methods in the .NET framework once you flip to the “All” tab in IntelliSense. So if you were wondering why the icons looked different now you know why :-). Extension methods are a great way to add additional functionality to a type without having to change the implementation of the actual class.

Now sometimes we’ll need to pass parameters and/or return a value from our extension method. Any parameters you specify after the first one in an extension method becomes a parameter the caller needs to pass. You can also return values from extension methods by declaring them as a Function instead.

 Imports System.Runtime.CompilerServices

Module CommonValidation
    ''' <summary>
    ''' This is my extension method that still does nothing. :-)
    ''' </summary>
    ''' <param name="value">extend the string type</param>
    ''' <param name="param">demoing parameter</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Extension()>
    Public Function MyMethod(ByVal value As String, param As Boolean) As Boolean
        'Do something
        Return True
    End Function
End Module

If we use this extension method now, you’ll see that a boolean parameter is required.

image

Now let’s start moving our validation methods in here. Let’s start with the State validation method. Notice in this method that we first format the value by making it upper case. In order to change the value of the type we’re extending we just pass the parameter ByRef instead of ByVal. I also want to allow passing of a parameter that indicates whether the field can be empty. So now here is our CommonValidation module with an IsState extension method:

 Imports System.Runtime.CompilerServices
Imports System.Text.RegularExpressions

Module CommonValidation
    ''' <summary>
    ''' Checks if the string is formatted as a 2 character US state code
    ''' </summary>
    ''' <param name="state">string type extension</param>
    ''' <param name="isEmptyOK">True if empty values are allowed, otherwise false</param>
    ''' <returns>True if the string is a valid US state, otherwise false</returns>
    ''' <remarks></remarks>
    <Extension()>
    Public Function IsState(ByRef state As String, ByVal isEmptyOK As Boolean) As Boolean
        If state <> "" Then
            'States should always be upper case
            state = state.ToUpper

            'Now validate based on regular expression pattern
            Dim pattern = "^(?:(A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|" +
                          "LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|P[AR]|RI|S[CD]|" +
                          "T[NX]|UT|V[AIT]|W[AIVY]))$"

            Return Regex.IsMatch(state, pattern)
        Else
            Return isEmptyOK
        End If
    End Function
End Module

Notice the nice IntelliSense we now get back in the _Validate methods when we call our extension method:

image

To finish off the State_Validate method all we need to do is check the return value to determine if we should add the property error or not:

 Private Sub State_Validate(results As EntityValidationResultsBuilder)
    If Not Me.State.IsState(True) Then
        results.AddPropertyError("Please enter a valid US State.")
    End If
End Sub

OK cool! So here are all of the extension validation methods in the module:

 Imports System.Runtime.CompilerServices
Imports System.Text.RegularExpressions

Module CommonValidation
    ''' <summary>
    ''' Checks if the string is formatted as a 2 character US state code
    ''' </summary>
    ''' <param name="state">string type extension</param>
    ''' <param name="isEmptyOK">True if empty values are allowed, otherwise false</param>
    ''' <returns>True if the string is a valid US state, otherwise false</returns>
    ''' <remarks></remarks>
    <Extension()>
    Public Function IsState(ByRef state As String, ByVal isEmptyOK As Boolean) As Boolean
        If state <> "" Then
            'States should always be upper case
            state = state.ToUpper

            'Now validate based on regular expression pattern
            Dim pattern = "^(?:(A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|" +
                          "LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|P[AR]|RI|S[CD]|" +
                          "T[NX]|UT|V[AIT]|W[AIVY]))$"

            Return Regex.IsMatch(state, pattern)
        Else
            Return isEmptyOK
        End If
    End Function
    ''' <summary>
    ''' Checks if the string is formatted as a valid US ZIP code
    ''' </summary>
    ''' <param name="zip">string type extension</param>
    ''' <param name="isEmptyOK">True if empty values are allowed, otherwise false</param>
    ''' <returns>True if the string is a valid ZIP code, otherwise false</returns>
    ''' <remarks></remarks>
    <Extension()>
    Public Function IsZIP(ByRef zip As String, ByVal isEmptyOK As Boolean) As Boolean
        If zip <> "" Then
            'Add the dash if the user didn't enter it and the ZIP code is 9 characters
            If Not zip.Contains("-") AndAlso zip.Length = 9 Then
                zip = zip.Substring(0, 5) + "-" + zip.Substring(5)
            End If
            'Now validate based on regular expression pattern
            Return Regex.IsMatch(zip, "^\d{5}$|^\d{5}-\d{4}$")
        Else
            Return isEmptyOK
        End If
    End Function
    ''' <summary>
    ''' Checks if the string is formatted as a Social Security Number
    ''' </summary>
    ''' <param name="ssn">string type extension</param>
    ''' <param name="isEmptyOK">True if empty values are allowed, otherwise false</param>
    ''' <returns>True if the string is a valid SSN, otherwise false</returns>
    ''' <remarks></remarks>
    <Extension()>
    Public Function IsSSN(ByRef ssn As String, ByVal isEmptyOK As Boolean) As Boolean
        If ssn <> "" Then
            'Add the dashes if the user didn't enter it and the SSN is 9 characters
            If Not ssn.Contains("-") AndAlso ssn.Length = 9 Then
                ssn = ssn.Substring(0, 3) + "-" + ssn.Substring(3, 2) + "-" + ssn.Substring(5)
            End If

            'Now validate based on regular expression pattern
            Return Regex.IsMatch(ssn, "^\d{3}-\d{2}-\d{4}$")
        Else
            Return isEmptyOK
        End If
    End Function

    ''' <summary>
    ''' Checks if the string contains only upper and lower case letters
    ''' </summary>
    ''' <param name="value">string type extension</param>
    ''' <param name="isEmptyOK">True if empty values are allowed, otherwise false</param>
    ''' <param name="isWhitespaceOK">True if spaces are allowed, otherwise false</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Extension()>
    Public Function IsAlpha(ByVal value As String,
                            ByVal isEmptyOK As Boolean,
                            ByVal isWhitespaceOK As Boolean) As Boolean
        If value <> "" Then
            'Validation for strings that must be Alphabetical characters only. 
            Dim pattern As String
            If isWhitespaceOK Then
                'Allows spaces 
                pattern = "^[a-zA-Z\s]+$"
            Else
                'No spaces
                pattern = "^[a-zA-Z]+$"
            End If

            Return Regex.IsMatch(value, pattern)
        Else
            Return isEmptyOK
        End If
    End Function

    ''' <summary>
    ''' Checks if the string contains only upper and lower case letters and/or numbers
    ''' </summary>
    ''' <param name="value">string type extension</param>
    ''' <param name="isEmptyOK">True if empty values are allowed, otherwise false</param>
    ''' <param name="isWhitespaceOK">True if spaces are allowed, otherwise false</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Extension()>
    Public Function IsAlphaNumeric(ByVal value As String,
                                   ByVal isEmptyOK As Boolean,
                                   ByVal isWhitespaceOK As Boolean) As Boolean
        If value <> "" Then
            'Validation for strings that must be AlphaNumeric characters only. 
            Dim pattern As String
            If isWhitespaceOK Then
                'Allows spaces 
                pattern = "^[a-zA-Z0-9\s]+$"
            Else
                'No spaces
                pattern = "^[a-zA-Z0-9]+$"
            End If

            Return Regex.IsMatch(value, pattern)
        Else
            Return isEmptyOK
        End If
    End Function
End Module

And finally, our customer entity that calls these methods. Notice how much cleaner the code is now and we can reuse these across any entity in the application.

 Namespace LightSwitchApplication
    Public Class Customer

        Private Sub State_Validate(results As EntityValidationResultsBuilder)
            If Not Me.State.IsState(True) Then
                results.AddPropertyError("Please enter a valid US State.")
            End If
        End Sub

        Private Sub SSN_Validate(results As EntityValidationResultsBuilder)
            If Not Me.SSN.IsSSN(True) Then
                results.AddPropertyError("Please enter a valid SSN (i.e. 123-45-6789).")
            End If
        End Sub

        Private Sub ZIP_Validate(results As EntityValidationResultsBuilder)
            If Not Me.ZIP.IsZIP(True) Then
                results.AddPropertyError("Please enter a valid US ZIP code.")
            End If
        End Sub

        Private Sub LastName_Validate(results As EntityValidationResultsBuilder)
            If Not Me.LastName.IsAlpha(False, True) Then
                results.AddPropertyError("Last Name can only contain alphabetical characters.")
            End If
        End Sub

        Private Sub FirstName_Validate(results As EntityValidationResultsBuilder)
            If Not Me.FirstName.IsAlpha(True, True) Then
                results.AddPropertyError("First Name can only contain alphabetical characters.")
            End If
        End Sub
    End Class
End Namespace

To pull this all together into a concrete example I’ve included the sample code here:

https://code.msdn.microsoft.com/Common-Validation-Rules-in-397bf46b

I hope this helps get you started writing your own more complex business rules and validation methods for your LightSwitch applications.

Enjoy!

Comments

  • Anonymous
    November 11, 2011
    Hi, Beth! Thank you to share with us this nice article. It's really a "clean code approach" to implement validation. As we are talking about validation, sometimes we just want to change the format of data entry. Yes, I'm talking about conditional format. For me, this feature is biggest gap of LightSwitch. Best regards.

  • Anonymous
    November 11, 2011
    Hi Ciro, You can also format the data exactly how you want based on any condition in the validation methods as well. But I think you are talking about changing the control UI, correct? Like changing the color or font based on a value, right? We've heard that before, feel free to vote for the feature here: visualstudio.uservoice.com/.../2132691-conditional-formating-for-lists-and-datagrids-and- Cheers, -Beth

  • Anonymous
    November 16, 2011
    I'm always surprise how you choose so good topics to present, and how good are your articles. So comprehensive, easy to read, educative. I can always learn something new. I'm really impressed. Best regards Spaso

  • Anonymous
    November 16, 2011
    One question Beth I can't find explanation enywhere, so maybe you can help. Using <property>_Validate method and using results.AddPropertyResult work well with all ValidationSeverity value (Error, Warning, Informational). That means that we have message on the screen "Validation issue exist..." But, when using <EntitySet>_Validate method, there is problem with Warning and Informational. To be clear, next code don't show any messages: partial void <TableSet>_Validate(<Table> entity, EntitySetValidationResultsBuilder results) { results.AddEntityResult("error", ValidationSeverity.Warning); } This stands also for ValidationSeverity.Informational (no message). Is this a bug a something else need to be done. Thanks in advanced. Spaso

  • Anonymous
    November 16, 2011
    Beth .. Awesome Post as usual i have one little question for you stackoverflow.com/.../best-way-to-change-font-properties-in-lightswitch thanks a lot in advance Kind Regards, Eng. 3oon

  • Anonymous
    November 16, 2011
    Hi.. this is nice style. i really like it.. can i have some format for asp .net.?

  • Anonymous
    November 18, 2011
    The comment has been removed

  • Anonymous
    December 21, 2011
    Hi Beth, If i create a new entry in an example, i get NullReferenceException, to avoiud this I have had to add a test IsNothing before the first If statement:            If Not IsNothing(Me.State) Then                If Me.State.Length > 0 Then                    Me.State = Me.State.ToUpper                End If            End If Is this correct, or did I miss some setting that solves this for me?

  • Anonymous
    December 30, 2011
    Hi,thank you for the article. I just want to ask if how can i use AllowSaveWithErrors method? I tried the ff. Private Sub QuotationHeader_AllowSaveWithErrors(ByRef result As Boolean)            result = False              'OR            Me.QuotationHeader_AllowSaveWithErrors(True)        End Sub

  • Anonymous
    February 29, 2012
    I am using the trial version of LS 2011 and the validation types are very limited...I do not have phone or email types available to me.  Can anyone help me understand why?

  • Anonymous
    May 14, 2012
    Does Silverlight support async validations, for e.g. if i wanted to make sure one email address is not in use. Can i check that against my database?

  • Anonymous
    May 15, 2012
    Hi Aswin, LightSwitch handles running the business rules you write at the right times on the right threads so you don't need to worry about this. If you want to check if the email address is not in use and it's a REQUIRED field on your entity, simply check the "Include in Unique Index" on the properties window. LS will automatically chck for duplicates. If the field is not required, create a modeled query (i.e. GetPersonByEmail) that has a filter parameter for the email. Then handle the Email_Validate method and do something like this: Private Sub Email_Validate(results As EntityValidationResultsBuilder)    If Me.Email <> "" Then        Dim duplicate = Me.DataWorkspace.ApplicationData.GetPersonByEmail(Me.Email).FirstOrDefault()        If duplicate IsNot Nothing Then           results.AddPropertyError("Another person already has this email address.")        End If    End If End Sub

  • Anonymous
    May 29, 2012
    Beth, How do you display a formatted zip code but not save it? For example 12345-6789 should be saved as 123456789. Thank you for your assistance

  • Anonymous
    October 17, 2012
    Hi Beth, Do you have a C# sample that shows how to do the extension methods for LightSwitch? Thanks, John

  • Anonymous
    November 05, 2012
    Converting VB.Net to C#, after converting CommonValidation.vb to c# via www.developerfusion.com/.../vb-to-csharp, I then did the following  - Deleted the "[Extension()]" above the methods  - Changed the first parameter from ref to this  - Removed the using System.Runtime.CompilerServices statement

  • Anonymous
    December 30, 2012
    Two upper is not working for me in C# see comments in my code below. using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.LightSwitch; using System.Diagnostics; using System.Text.RegularExpressions; namespace LightSwitchApplication {    public partial class PPIsheet    {        private void StateProvidenceHome(EntityValidationResultsBuilder results)        {            // results.AddPropertyError("<Error-Message>");            if (!string.IsNullOrEmpty(this.StateProvinceHome))            {                //TwoUpper Code Not working cannot Call TwoUpper for some reason.                //Error Says " 'string' does not contain a definition for 'Two Upper'                //and no extention method 'TwoUpper' accepting a first argument of type 'string'                //cound be found (are you missing a usign directive of an assenbly reference?                this.StateProvinceHome = this.StateProvinceHome.TwoUpper;                dynamic pattern = "^(?:(A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|" +                                               "LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|P[AR]|RI|S[CD]|" +                                               "T[NX]|UT|V[AIT]|W[AIVY]))$";                if (!Regex.IsMatch(this.StateProvinceHome, pattern))                {                    results.AddPropertyError("Please enter a valid US State.");                }            }        }    } }

  • Anonymous
    January 06, 2013
    C# using System.Text.RegularExpressions; namespace LightSwitchApplication {    public static class CommonValidation    {        public static bool MyMethod(this string value, bool param)        {            //Do something            return true;        }        public static bool IsState(this string state, bool isEmptyOK)        {            if (!string.IsNullOrEmpty(state))            {                //States should always be upper case                state = state.ToUpper();                //Now validate based on regular expression pattern                dynamic pattern = "^(?:(A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|" +                    "LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|P[AR]|RI|S[CD]|" +                    "T[NX]|UT|V[AIT]|W[AIVY]))$";                return Regex.IsMatch(state, pattern);            }            else            {                return isEmptyOK;            }        }        public static bool IsZIP(this string zip, bool isEmptyOK)        {            if (!string.IsNullOrEmpty(zip))            {                //Add the dash if the user didn't enter it and the ZIP code is 9 characters                if (!zip.Contains("-") && zip.Length == 9)                {                    zip = zip.Substring(0, 5) + "-" + zip.Substring(5);                }                //Now validate based on regular expression pattern                return Regex.IsMatch(zip, "^\d{5}$|^\d{5}-\d{4}$");            }            else            {                return isEmptyOK;            }        }        public static bool IsSSN(this string ssn, bool isEmptyOK)        {            if (!string.IsNullOrEmpty(ssn))            {                //Add the dashes if the user didn't enter it and the SSN is 9 characters                if (!ssn.Contains("-") && ssn.Length == 9)                {                    ssn = ssn.Substring(0, 3) + "-" + ssn.Substring(3, 2) + "-" + ssn.Substring(5);                }                //Now validate based on regular expression pattern                return Regex.IsMatch(ssn, "^\d{3}-\d{2}-\d{4}$");            }            else            {                return isEmptyOK;            }        }    } }

  • Anonymous
    January 06, 2013
    C# using System.Text.RegularExpressions; namespace LightSwitchApplication {    public static class CommonValidation    {        public static bool MyMethod(this string value, bool param)        {            //Do something            return true;        }        public static bool IsState(this string state, bool isEmptyOK)        {            if (!string.IsNullOrEmpty(state))            {                //States should always be upper case                state = state.ToUpper();                //Now validate based on regular expression pattern                dynamic pattern = "^(?:(A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|" +                    "LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|P[AR]|RI|S[CD]|" +                    "T[NX]|UT|V[AIT]|W[AIVY]))$";                return Regex.IsMatch(state, pattern);            }            else            {                return isEmptyOK;            }        }        public static bool IsZIP(this string zip, bool isEmptyOK)        {            if (!string.IsNullOrEmpty(zip))            {                //Add the dash if the user didn't enter it and the ZIP code is 9 characters                if (!zip.Contains("-") && zip.Length == 9)                {                    zip = zip.Substring(0, 5) + "-" + zip.Substring(5);                }                //Now validate based on regular expression pattern                return Regex.IsMatch(zip, "^\d{5}$|^\d{5}-\d{4}$");            }            else            {                return isEmptyOK;            }        }        public static bool IsSSN(this string ssn, bool isEmptyOK)        {            if (!string.IsNullOrEmpty(ssn))            {                //Add the dashes if the user didn't enter it and the SSN is 9 characters                if (!ssn.Contains("-") && ssn.Length == 9)                {                    ssn = ssn.Substring(0, 3) + "-" + ssn.Substring(3, 2) + "-" + ssn.Substring(5);                }                //Now validate based on regular expression pattern                return Regex.IsMatch(ssn, "^\d{3}-\d{2}-\d{4}$");            }            else            {                return isEmptyOK;            }        }        public static bool IsAlpha(this string value, bool isEmptyOK, bool isWhitespaceOK)        {            if (!string.IsNullOrEmpty(value))            {                //Validation for strings that must be Alphabetical characters only.                string pattern = null;                if (isWhitespaceOK)                {                    //Allows spaces                    pattern = "^[a-zA-Z\s]+$";                }                else                {                    //No spaces                    pattern = "^[a-zA-Z]+$";                }                return Regex.IsMatch(value, pattern);            }            else            {                return isEmptyOK;            }        }     } }

  • Anonymous
    March 12, 2013
    Hi Beth! Thanks for sharing your valuable knowledge. I'm developing a system in C # using Visual Studio 2012. I read part of the article that shows how to create a module validation (Creating a Common Validation Module) and did not see the option to add a module, as explained. Could you help? thank you

  • Anonymous
    June 18, 2013
    Beth, do you have the C# version of this demo?