21 November 2013

jQuery Syntax

The jQuery syntax is tailor made for selecting HTML elements and performing some action on the element(s).

Basic syntax is: $(selector).action()
  • A $ sign to define/access jQuery
  • A (selector) to "query (or find)" HTML elements
  • A jQuery action() to be performed on the element(s)
Examples:

$(this).hide() - hides the current element.
$("p").hide() - hides all <p> elements.
$(".test").hide() - hides all elements with class="test".
$("#test").hide() - hides the element with id="test".

The Document Ready Event

You might have noticed that all jQuery methods in our examples, are inside a document ready event:

ex :   $(document).ready(function() {

                //  jquery methods......
        });

This is to prevent any jQuery code from running before the document is finished loading (is ready).
It is good practice to wait for the document to be fully loaded and ready before working with it. This also allows you to have your JavaScript code before the body of your document, in the head section.
Here are some examples of actions that can fail if methods are run before the document is fully loaded:
  • Trying to hide an element that is not created yet
  • Trying to get the size of an image that is not loaded yet

JQuery Basics

JQuery Basics :
  • jQuery is a JavaScript Library.
  • jQuery greatly simplifies JavaScript programming.
  • jQuery is easy to learn.
    Before you start studying jQuery, you should have a basic knowledge of:
  • HTML
  • CSS
  • JavaScript

What is jQuery?

  • jQuery is a lightweight, "write less, do more", JavaScript library.
  • The purpose of jQuery is to make it much easier to use JavaScript on your website.
  • jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code.
  • jQuery also simplifies a lot of the complicated things from JavaScript, like AJAX calls and DOM manipulation.


The jQuery library contains the following features:
  • HTML/DOM manipulation
  • CSS manipulation
  • HTML event methods
  • Effects and animations
  • AJAX
  • Utilities
Tip: In addition, jQuery has plugins for almost any task out there.

Downloading jQuery :

There are two versions of jQuery available for downloading:
  • Production version - this is for your live website because it has been minified and compressed
  • Development version - this is for testing and development (uncompressed and readable code)
The jQuery library is a single JavaScript file, and you reference it with the HTML <script> tag (notice that the <script> tag should be inside the <head> section):

ex:  <head>
         <script src="jquery-1.10.2.min.js"></script>
       </head>

Alternatives to Downloading

Both Google and Microsoft host jQuery.

To use jQuery from Google or Microsoft, use one of the following:

ex : <head>
            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
            </script>
     </head>




Explain about Custom Validator control in asp.net

In this post we will discuss about Custom Validator control in asp.net.

You can also check my previous articles on RequiredFieldValidator, RangeValidator, RegularExpressionValidator,comparevalidator control examples in Asp.Net.

The CustomValidator control allows you to write a method to handle the validation of the value entered.

Properties :
1.Display : The display behavior for the validation control. Legal values are:
  • None (the control is not displayed. Used to show the error message only in the ValidationSummary control)
  • Static (the control displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation.
  • Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation
2.Error Message :
The text to display in the ValidationSummary control when validation fails. Note:This text will also be displayed in the validation control if the Text property is not set.

code :

<script  runat="server">
Sub user(source As object,args As ServerValidateEventArgs)
   if len(args.Value)<8 or len(args.Value)>16 then
    args.IsValid=false
   else
    args.IsValid=true
   end if
End Sub
</script>    

<!DOCTYPE html>
<html>
<body>

<form runat="server">
<asp:Label runat="server" Text="Enter a username: " />
<asp:TextBox id="txt1" runat="server" />
<asp:Button Text="Submit" runat="server"/>
<br>
<asp:Label id="mess" runat="server"/>
<br>
<asp:CustomValidator
ControlToValidate="txt1"
OnServerValidate="user"
Text="A username must be between 8 and 16 characters!"
runat="server"/>
</form>

</body>
</html>


Declare two Label controls, one TextBox control, one Button control, and one CustomValidator control in an .aspx file. The user() function checks the length of the input value. If the length is <8 or >16 the text "A username must be between 8 and 16 characters!" will appear in the CustomValidator control as shown below :







20 November 2013

Explain about Compare validator in asp.net

In this post we will discuss about Compare validator in asp.net.

You can also check my previous articles on RequiredFieldValidator, RangeValidator, RegularExpressionValidator ,Validation summary examples in Asp.Net.

The CompareValidator control is used to compare the value of one input control to the value of another input control or to a fixed value.
Note : if the input control is empty,the validation will succed.use the requiredfieldvalidator control to make the field required.

Properties :
below operators using for compareValidator
1.equal
2.GreaterThan
3.GreaterThanEqual
4.LessThan
5.LessThanEqual
6.NotEqual
7.DataTypeCheck

Specifies the datatype of the values to compare.The types are ;

  • currency
  • Date
  • Double
  • Integer
  • String


Below is the full HTML code

<!DOCTYPE html>
<html>
<body>

<form runat="server">
<table border="0" bgcolor="#b0c4de">
   <tr style="vertical-align:top">
     <td colspan="4"><h4>Compare two values</h4></td>
   </tr>
   <tr style="vertical-align:top">
     <td><asp:TextBox id="txt1" runat="server" /></td>
     <td> = </td>
     <td><asp:TextBox id="txt2" runat="server" /></td>
     <td><asp:Button Text="Validate" runat="server" /></td>
   </tr>
</table>
<br>
<asp:CompareValidator
id="compval"
Display="dynamic"
ControlToValidate="txt1"
ControlToCompare="txt2"
ForeColor="red"
BackColor="yellow"
Type="String"
EnableClientScript="false"
Text="Validation Failed!"
runat="server" />
</form>

</body>
</html>


Declare two TextBox controls,one Button control and one compareValidator in an .aspx file.
If validation fails,the text " Validation Failed! " will be displayed in red on a yellow background in the CompareValidator control as shown in figure below :



ValidationGroup in Asp.Net

In this post we will discuss how to use ValidationGroup in Asp.Net.

You can also check my previous posts on  ValidationSummary, RegularExpressionValidator, RangeValidator, RequiredFieldValidator example in Asp.Net.

If your page has several groups of controls and you want to perform validation separatly then ValidationGroup will be very much helpful.

You have to assign ValidationGroup to controls, validators and buttons. Here in this example we have two pannels.

Below is the .aspx code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ValidatorExamples.aspx.cs"
    Inherits="ValidatorExamples" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ValidationGroup example in Asp.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Panel ID="pnlLogin" runat="server">
            Enter EmailID:<asp:TextBox ID="txtEmailIDLogin" runat="server" ValidationGroup="Group1"></asp:TextBox><br />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ForeColor="Red"
                ValidationGroup="Group1" ErrorMessage="Enter Email ID!" ControlToValidate="txtEmailIDLogin"></asp:RequiredFieldValidator><br />
            <asp:Button ID="btnLogin" runat="server" Text="Submit" ValidationGroup="Group1" />
        </asp:Panel>
        <asp:Panel ID="pnlSignup" runat="server">
            Enter EmailID: <asp:TextBox ID="txtEmailIDSignUp" runat="server" ValidationGroup="Group2"> </asp:TextBox>
            <br />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ForeColor="Red"  ErrorMessage="Enter Email ID!" ControlToValidate="txtEmailIDSignUp" ValidationGroup="Group2"></asp:RequiredFieldValidator>
<br />
            <asp:Button ID="btnSignup" runat="server" Text="Submit" ValidationGroup="Group2"/>
        </asp:Panel>
    </div>
    </form>
</body>
</html>

Here if you click on first button then only 1st textbox will get validated. And also if you click on second button then 2nd textbox will get validated like below figure.