Reader Level:
ARTICLE

Validation of Mobile Internet

Posted by Puran Mehra Articles | Mobile & Embedded April 21, 2010
In this article I will explain you about Validation of Mobile Internet.
  • 0
  • 0
  • 5114

This article has been excerpted from book "The Complete Visual C# Programmer's Guide" from the Authors of C# Corner.

Up to this point we have discussed the user interface in terms of visible controls, but any data an application collects from the user will require some form of validation. Validation is an essential part of any program, and .Net provides a number of validation controls.

Let's look at the basic properties of a validation control:

  • The ControlToValidate property allows the developer to associate a graphical control with a validation control. It gets or sets the ID of a control that needs to be validated.
  • The ErrorMessage property allows the developer to specify the error message text she wants displayed.
  • The Display property is used to set whether the ErrorMessage property is to be shown statically or dynamically.

The preceding basic properties apply to all of the validation controls. There are a total of five validation controls, which all inherit from the BaseValidator class. Let's discuss them one by one.

RequiredFieldValidator

Required field validation takes a control where a value must be provided. The field to be validated cannot be empty. Syntactically the RequiredFieldValidator looks like the following:

            <mobile:requiredfieldvalidator controltovalidate="validator1" runat="server">
Required Field

</
mobile:requiredfieldvalidator>

RangeValidator


The RangeValidator control ensures that the value of the control to be validated falls within a specified range by providing the Minimum and Maximum properties.


            <mobile:rangevalidator controltovalidate="validator2" type="Integer" maximumvalue="75"
                minimumvalue="50" runat="server">
Does not fall in the specified Range

</
mobile:rangevalidator>

Listing 25.5 contains an example of these validators.

Listing 25.5: Example of Validation Controls


<%
@ Page Inherits="System.Web.UI.MobileControls.MobilePage" Language="C#" %>
<%
@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %>
<script language="c#" runat="server">
    protected void Submit_OnClick(Object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            ActiveForm = Form2;
        }
    }

</
script>
<
mobile:form id="Form1" runat="server">
<
mobile:Label runat="server">
Enter your Name

</
mobile:Label>
<
mobile:TextBox id="validator1" runat="server"/>
<
mobile:RequiredFieldValidator ControlToValidate="validator1"
runat
="server">
Required Field

</
mobile:RequiredFieldValidator>
<
mobile:Label runat="server">
Enter Age

</
mobile:Label>
<
mobile:TextBox id="validator2" runat="server"/>
<
mobile:RangeValidator ControlToValidate="validator2"
Type
="Integer"
MaximumValue
="75"
MinimumValue
="50"
runat
="server">
Your Age does not Fall in the specified Range

</
mobile:RangeValidator>
<
mobile:RequiredFieldValidator ControlToValidate="validator2"
runat
="server">
Required Field

</
mobile:RequiredFieldValidator>
<
mobile:Command runat="server" OnClick="Submit_OnClick">
Submit

</
mobile:Command>
</
mobile:form>
<
mobile:form id="Form2" runat="server">
<
mobile:Label runat="server">
Your Entry is Submited

</
mobile:Label>
</
mobile:form>

The output, when you submit this page, will look something like that shown in Figures 24.7 and 24.8.

Figure-24.7.gif

Figure 24.7: Error Message Displayed If Form Is Submitted Without Input

Figure-24.8.gif

Figure 24.8: Error Message Displayed If Input Is Outside the Specified Range

If the IsValid property is missing, the page will be submitted in spite of providing the control with validation. Therefore, whenever you use a validation control make sure the Page.IsValid property is present to ensure that validation has taken place.

RegularExpressionValidator

The RegularExpressionValidator is used to validate a control using a "regular expression."

To check and make sure the "@" is present in an e-mail address, you would use the RegularExpressionValidator in the following manner:


<
mobile:RegularExpressionValidator ControlToValidate="EmailField"
ValidationExpression
=".*@.*"
runat
="server">

CompareValidator

The CompareValidator control is used when you want to compare a specified control with another control or value using the Operator property. The Operator property supports enumerations such as GreaterThan, LessThan, or EqualTo.

There are two properties to perform this: ControlToCompare and ValueToCompare. In the first case, you can specify any control you want to validate, as in the case of a password field. When you submit a Confirm Password field, you can validate it against another field called Choose Password for equality. Here's an example of this type of validation:

<
mobile:CompareValidator ControlToValidate="ConfirmPassword"
Type
="String"
Operator
="Equal"
ControlToCompare
="ChoosePassword"
runat
="server">

The Password doesn`t Match

</
mobile:CompareValidator>

In the second case, you can specify the value with which you want a control to be compared:


<
mobile:CompareValidator ControlToValidate="validator4"
Type
="Integer"
Operator
="GreaterThan"
ValueToCompare
="20"
runat
="server">
The value is not in the Range

</
mobile:CompareValidator>

CustomValidator

When you want a control to be validated using your own rules, or you want the value to be checked in a specific way, you can customize the way the control is validated.


<
mobile:CustomValidator ControlToValidate="validator5"
OnServerValidate
="Custom_Validator"
runat
="server">
Invalid Response

</
mobile:CustomValidator>

This particular validation takes place on the server by means of the Custom_Validator method. In that function, you can write the required validation code.

ValidationSummary

We have seen all the major validators, but let's say you do not want an error message to be displayed for every validation that fails. There is a way to accommodate that.

The ValidationSummary control will list all the errors that occur in the form. This control has the ability to filter error messages by only displaying the text of the ErrorMessage property.


<
mobile:ValidationSummary FormToValidate="Form1"
HeaderText
="Errors Are:" runat="server">
</
mobile:ValidationSummary>

Listing 24.8 gives you a fair picture of a validation summary.

Listing 24.8: Example of Validation Controls 2


<%
@ Page Inherits="System.Web.UI.MobileControls.MobilePage" Language="C#" %>
<%
@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %>

<
script language="c#" runat="server">
    protected void Submit_OnClick(Object sender, EventArgs e)
    {
        ActiveForm = Form2;
    }

    void Custom_Validate(object source, ServerValidateEventArgs args)
    {
        int number = Int32.Parse(SecurityCode.Text);
        if (number % 5 == 0)
            args.IsValid = true;
        else
            args.IsValid = false;
    }

</
script>
<
mobile:form id="Form1" runat="server">
<
mobile:Label runat="server">
Enter EmailId

</
mobile:Label>
<
mobile:TextBox id="EmailId" runat="server"/>
<
mobile:RegularExpressionValidator ControlToValidate="EmailId"
ValidationExpression
=".*@.*"
runat
="server" ErrorMessage="Invalid Email ID">
</
mobile:RegularExpressionValidator>
<
mobile:Label runat="server">
Enter Password

</
mobile:Label>
<
mobile:TextBox id="EnterPass" runat="server"/>
<
mobile:Label runat="server">
Confirm Password

</
mobile:Label>
<
mobile:TextBox id="ConfirmPass" runat="server"/>
<
mobile:CompareValidator ControlToValidate="ConfirmPass"
Type
="String"
Operator
="Equal"
ControlToCompare
="EnterPass"
runat
="server" ErrorMessage="The Password doesn`t Match">
</
mobile:CompareValidator>
<
mobile:Label runat="server">
Enter Security Code

</
mobile:Label>
<
mobile:TextBox id="SecurityCode" runat="server"/>
<
mobile:CustomValidator ControlToValidate="SecurityCode"
OnServerValidate
="Custom_Validate"
runat
="server" ErrorMessage=" Code Not Permitted">
</
mobile:CustomValidator>
<
mobile:Command runat="server" OnClick="Submit_OnClick">
Submit

</
mobile:Command>
</
mobile:form>
<
mobile:form id="Form2" runat="server">
<
mobile:Label runat="server">
Valid if its empty below
</mobile:Label>
<
mobile:ValidationSummary FormToValidate="Form1"
HeaderText
="Error listed below:"
runat
="server"/>
</
mobile:form>

The code in Listing 24.8 will produce the display shown in Figures 24.9 and 24.10 when the text fields are incorrectly filled out.

Figure-24.9.gif

Figure 24.9: Screen for the User's Input

Figure-24.9.gif

Figure 24.10: Error Messages that Make Use of ValidationSummary Control

Conclusion

Hope this article would have helped you in understanding Validation of Mobile Internet. See other articles on the website on .NET and C#.

visual C-sharp.jpg The Complete Visual C# Programmer's Guide covers most of the major components that make up C# and the .net environment. The book is geared toward the intermediate programmer, but contains enough material to satisfy the advanced developer.

COMMENT USING

Trending up