Vectorlight News

  • Chat App Converted to HTML and JQuery
    Sep 08, 2011

    Converted from Silverlight to HTML and Javascript/JQuery is the Vectorlight Chat App. Login using your Vectorlight password to chat using your username and avatar.

  • HTML5 iPhone,Android Big Guns Tower Defense
    Jul 02, 2011

    Big Guns has made the leap from Windows Phone 7 (XNA) to HTML5 so you can now play it on your iPhone, Android and other HTML5 compatible devices.

  • HTML5 Games - Word Poppers and Batty
    Jun 04, 2011

    As the take-up of HTML5 quickens (74% of users currently have a browser capable of HTML5 Canvas) we present two more games for both your browser and mobile.

  • Big Guns Tower Defense on Windows Phone 7
    May 06, 2011

    Coming soon to Windows Phone 7 is an XNA port of the popular Vectorlight tower defense game Super Tower Defense. Whilst retaining many of the graphical and gameplay features of the original Silverlight game.

  • Wakacube WP7 Update
    Apr 26, 2011

    Released to the Windows Phone 7 marketplace today is Version 1.1 of Wakacube the 3D physics game of skill. Included in the update are more levels (30 in total) and new mode Wakatime which generates random crate structures to keep players entertained long after the levels have been completed.

  • Home Page News

Validating Controls in Silverlight

If ever you have done validation of form controls in ASP.NET or Desktop applications then validating controls in Silverlight will be easy to get to grips with.  Unfortunately there are no controls in Silverlight that perform validation, maybe Microsoft will release some at some point but for now we have a few tips for validating and a simple example Registration Form with basic validation.

Our example Silverlight registration dialog


The XAML for the above dialog is as follows:

<UserControl x:Class="SilverlightApplication1.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:liquid="clr-namespace:Liquid;assembly=Liquid"
    Width="400" Height="300">
    <Canvas>
        <Rectangle Width="330" Height="170" RadiusX="7" RadiusY="7" Fill="#44888888" />

        <Rectangle Canvas.Left="2" Canvas.Top="2" Width="326" Height="166" RadiusX="5" RadiusY="5" StrokeThickness="2" Stroke="#888888">
            <Rectangle.Fill>
                <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                    <GradientStop Color="#ffdfdfdf" Offset="0.0" />
                    <GradientStop Color="#fff8f8f8" Offset="0.7" />
                    <GradientStop Color="#ffeeeeee" Offset="1.0" />
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>
        
        <Rectangle Canvas.Left="5" Canvas.Top="5" Width="320" Height="21" StrokeThickness="0.5" Stroke="#7A8295" RadiusX="3" RadiusY="3">
            <Rectangle.Fill>
                <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                    <GradientStop Color="#626C88" Offset="0.0" />
                    <GradientStop Color="#393F4D" Offset="0.5" />
                    <GradientStop Color="#151516" Offset="0.5" />
                    <GradientStop Color="#3C476F" Offset="1.0" />
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>
        
        <TextBlock Text="Username:" Canvas.Left="10" Canvas.Top="35" />
        <TextBox x:Name="newUsername" Canvas.Left="120" Canvas.Top="32" Width="200" TabIndex="0" />
        <TextBlock Text="Email:" Canvas.Left="10" Canvas.Top="65" />
        <TextBox x:Name="newEmail" Canvas.Left="120" Canvas.Top="62" Width="200" TabIndex="1" />
        <TextBlock Text="Password:" Canvas.Left="10" Canvas.Top="95" />
        <TextBox x:Name="newPassword" Canvas.Left="120" Canvas.Top="92" Width="200" TabIndex="2" />

        <HyperlinkButton x:Name="registerHelp" Canvas.Left="10" Canvas.Top="146" NavigateUri="help.aspx" Content="Problems Registering?" />
        <TextBlock x:Name="registerStatus" Canvas.Left="10" Canvas.Top="121" Foreground="Red" FontSize="10" FontWeight="Bold" />

        <Button Canvas.Left="200" Canvas.Top="133" Width="50" Content="Cancel" Click="RegisterCancel_Click" />
        <Button x:Name="registerRegister" Canvas.Left="260" Canvas.Top="133" Width="60" Content="Register" Click="Register_Click" />
    </Canvas>
</UserControl>


As you can see in our XAML we define three TextBox controls, one for our Username, Email address and Password.  The validation we are going to perform is fairly simple, in our C# we will write an event handler for the Click event of the Login button and perform our validation there.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication1
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
        }

        private void RegisterCancel_Click(object sender, RoutedEventArgs e)
        {
            // Registration has been cancelled. Handle this here
        }

        private void Register_Click(object sender, RoutedEventArgs e)
        {
            // Clear the status text
            registerStatus.Text = "";

            if (newPassword.Text.Length == 0)
            {
                // No password entered
                registerStatus.Text = "Enter a password.";
                newPassword.Focus();
            }
            if (newEmail.Text.Length == 0)
            {
                // No email address entered
                registerStatus.Text = "Enter an email.";
                newEmail.Focus();
            }
            else if (!Regex.IsMatch(newEmail.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"))
            {
                // An invalid email format was entered
                registerStatus.Text = "Enter a valid email.";
                newEmail.Select(0, newEmail.Text.Length);
                newEmail.Focus();
            }
            if (newUsername.Text.Length == 0)
            {
                // No username was entered
                registerStatus.Text = "Enter a username.";
                newUsername.Focus();
            }

            // Return if the status text is not empty
            if (registerStatus.Text.Length > 0)
            {
                return;
            }

            // All done, the 3 fields have been validated
            // Do your registration processing here....
        }
    }
}


As you can see from the code we ensure the fields are not empty before performing an extra check on the email address field to see if the string entered "looks" like an email address.  This will stop most typing mistakes by the user but, as there is no way to completely validate the email, you should implement your own additional steps to validate the email address.

Your Comments

Sundeep Raina posted

how can we validate silverlight from backend.



Regards


sundeep Raina


R.Rajesh posted

Hai Superb Article From You and Go ahead.We Expect more from Your Side Continue


wasim1623 posted

your coding is best.i love your coding


Kavita posted

How to validate more than one control at time...


anurag pal posted

Hi,


  I am beginner in silverlight application.and  I am confuse about x:name .can you please tell me the role of x:name in above code.


thank you


Venkadesh Babu posted

Thanksssssssss !!! Ur coding is working. very excellent...thank u so much...


Yana posted

Thanks for providing sample.


This is very useful for beginners...


vinaypinisetti posted

thank you very much to post this dan


venkatesh posted

Thanks for providing precision sample for the beginners.........


BigTiger posted

This is how the code will eventually be. But for now, Microsoft introduded something called MVVM. I know MVVM will eventually fail, but for now, you will still have to say that you like MVVM.


 

Post your Comments

Rate this: 

1 Star 2 Star 3 Star 4 Star 5 Star
39 Ratings / 3.9 Average

Tweets

Silverlight Controls

  • Rich TextBox

    Create and edit rich content with this slick and expandable Rich TextBox...

  • TreeView

    This easy to use TreeView comes with drag and drop, sorting, searching and much more...

  • Context Menu

    You too can have cool popup context menus in your Silverlight applications...

  • Resizable Dialog

    Draggable and resizable popup dialogs are what serious Silverlight developers need...

  • Spell Checker

    Real-time spell checking in Silverlight? We did it first here...