Monday, 21 January 2013

Forcing html Input element to Numeric Only Input

While developing a shopping cart function for a client, I needed to restrict the user to entering numbers only in an Input field

I came across this handy post;

http://www.itjungles.com/javascript/how-to-use-javascript-to-force-numeric-value-in-textbox

Basically, we need to add the following function to the JavaScript section;

function isNumericKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)) 
    {
        return false;
    }
    else
    {
        return true;
    }
}    


We then add the the following to your input element;


onkeypress="return isNumericKey(event)"


Hey Presto! You have a numeric only Input Element!

Monday, 7 January 2013

MVVM WPF Validating multiple items together

In an application I am developing, I required that two checkboxes be validated together, when either one of them were clicked. I also needed to only show the Validation error once in my Error Message Box.

To achieve this we first call the OnPropertyChanged routine, which in turn raises the PropertyChanged event, for each linked Property when any of the linked properties change.

For example;

Public Property Property1 As Boolean
    Get
        Return _Property1
    End Get
    Set(value As Boolean)
 
        If _Property1 <> value Then
 
            _Property1 = value
            OnPropertyChanged("Property1")
            OnPropertyChanged("Property2")
 
        End If
 
    End Set
End Property


For the Validation errors, I have employed a simple Key, Value Pair list to store all of the Validation Error Results. Thus, I simply check to see if the other Property has Registered an Error, and remove it if so;



 
        ''' <summary>
        ''' Validates the Current Item
        ''' </summary>
        ''' <param name="ColumnName"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides Function ValidateItemValue(ColumnName As String) As String
            Dim strResult As String = ""
 
            Select Case ColumnName
 
                Case "Property1"
 
                    If Property1 = False And Property2 = False Then
 
                        strResult = "Please select either Property1, Propert2 or Both!"
 
                        If Me.lstErrors.ContainsKey("Property2") = True Then
 
                            Me.lstErrors.Remove("Property2")
 
                        End If
 
                    End If
 
                Case "Property2"
 
                    If Property1 = False And Property2 = False Then
 
                        strResult = "Please select either Property1, Property2 or Both!"
 
                        If Me.lstErrors.ContainsKey("Property1") = True Then
 
                            Me.lstErrors.Remove("Property1")
 
                        End If
 
                    End If
 
 
            End Select
 
            SortErrorList(ColumnName, strResult)        ' Add or Remove an Error Item
 
            Return strResult
 
        End Function

Instantiating a Generic Type of T

I found a requirement recently where I needed to Instantiate a new instance of a Generic Type.

Ordinarily we would declare a new instance of a class using a Generic Type using something like;

Public Class ViewModel_Class(Of T)

However, If we then try to create a new Object of Type T by performing the following…



Dim MyNewObject As New T

…an error will be produced, reporting “New cannot be used on a type parameter that does not have a new constraint”.

This error is telling us that the Compiler doesn’t know if the Generic Type has an implicit “New” Sub, which is can use to Instantiate a new Object.

So, we need to add a Generic Type Constraint for a New Sub by specifying this when we declare our Generic Type;



Public Class ViewModel_Class(Of T as {New})

This tells the compiler that whatever Type T is, it will have a New Sub which it can use to instantiate the New Object.

You can read more on this subject on the Microsoft Website at;

http://msdn.microsoft.com/en-us/library/w256ka79(v=vs.110).aspx