Showing posts with label JQuery contorls. Show all posts
Showing posts with label JQuery contorls. Show all posts

Saturday, 25 August 2012

Adding HTML control dynamically using JQuery

In this article I will explain how to add HTML control dynamically using jQuery and then retrieve values from the dynamically generated controls.

You have seen many website where you find provision to add multiple mobile numbers, interests, subject or some other type of data by clicking Add button (this add new row with required fields).

In many websites this is done using Ajax ( i.e. control generated on server side and then rendered asynchronously on the client browser). The drawback of this is every time async postback happen to server and this overhead, also in some website the browser become very slow in cases it hang as well after 15-20 postbacks.

Well in jquery it is wery simple just by using appent method you can add the new DOM element inside existing DOM element like <div>.

Code to add HTML control

HTML:

<div id="mynewDiv" style="width:auto; height:auto"></div>
<input type="button" value="Add Mobile Number" id="btnNew"/>&nbsp;
<input type="button" value="Check Values" id="btnCheck"/>


JavaScript (jQuery):

$("#btnNew").click(function () {
    $("#mynewDiv").append("<div style='padding: 5px 5px 5px 5px;'><label>Mobile Number: </label>&nbsp;<input  type='text' width='200px' class='txtTelephone' /></div>")
});


In above code on clicking of "Add Mobile Number" button a new textbox with label is added to "mynewDiv" panal.
Now to read the values from the textbox added dynamically use below code:

$("#btnCheck").click(function () {
    $(".txtTelephone").each(function (i) {
        alert($(this).val());
    });
});


The complete javascript code look like:

<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#btnNew").click(function () {
            $("#mynewDiv").append("<div style='padding: 5px 5px 5px 5px;'><label>Mobile Number: </label>&nbsp;<input  type='text' width='200px' class='txtTelephone' /></div>")
        });

        $("#btnCheck").click(function () {
            $(".txtTelephone").each(function (i) {
                alert($(this).val());
            });
        });
    });
</script>

Monday, 19 March 2012

JQuery Password Strength indicator Example in asp.net | Display Validation error message with images in asp.net

Introduction:

In this article I will explain how to display images whenever validation fails in form using JQuery and I will explain how to show password strength using JQuery in asp.net.



Description:
  
In previous articles I explained about form validations using JavaScript  and Ajax Password Strength using asp.net . Now I will explain how to show the images when validation fails and I will explain how to show the password strength using JQuery in asp.net.
In one of the website I saw different type of form validations in registration form when we click on submit button without entering any data they are displaying error message with image just beside of particular control and they are displaying password strength based on text entered in textbox. After seen that form I decided to write post to explain how to implement this one using JQuery.

To implement this concept first open Visual Studio and create new website after that right click on your website and add two new folders and give name as Images and JS and insert images and script files in particular folders you should get it from attached folder. After that write the following code in your aspx page 

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>JQuery Validate Form and Show Password Strength</title>
<link href="FormValidation.css" rel="stylesheet" type="text/css" />
<link href="jquery.validate.password.css" rel="stylesheet" type="text/css" />
<script src="JS/jquery.js" type="text/javascript"></script>
<script src="JS/jquery.validate.js" type="text/javascript"></script>
<script src="JS/jquery.validate.password.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">

$(document).ready(function() {
// validate signup form on keyup and submit
var validator = $("#signupform").validate({
rules: {
username: {
required: true,
minlength: 2
},
password: {
password: "#username"
},
password_confirm: {
required: true,
equalTo: "#password"
},
location: {
required: true
}
},
messages: {
username: {
required: "Enter a username",
minlength: jQuery.format("Enter at least {0} characters")
},
password_confirm: {
required: "Repeat your password",
minlength: jQuery.format("Enter at least {0} characters"),
equalTo: "Enter the same password as above"
},
location: {
required: "Enter a Location"
}
},
// the errorPlacement has to take the table layout into account
errorPlacement: function(error, element) {
error.prependTo(element.parent().next());
},
// set this class to error-labels to indicate valid fields
success: function(label) {
// set &nbsp; as text for IE
label.html("&nbsp;").addClass("checked");
}
});
});
</script>
</head>
<body>
<form id="signupform" runat="server">
<div id="signupwrap">
<table align="center">
<tr>
<td></td>
<td><b>User Registration</b></td>
<td></td>
</tr>
<tr>
<td align="right" class="label">UserName:</td>
<td  class="field"><asp:TextBox ID="username" runat="server"/></td>
<td class="status"></td>
</tr>
<tr>
<td  align="right" class="label">Password:</td>
<td  class="field"><asp:TextBox ID="password" runat="server" TextMode="Password"/></td>
<td class="status" align="left">
<div class="password-meter">
<div class="password-meter-message">&nbsp;</div>
<div class="password-meter-bg">
<div class="password-meter-bar"></div>
</div>
</div>
</td>
</tr>
<tr><td align="right" class="label">Confirm Password:</td>
<td  class="field"><asp:TextBox ID="password_confirm" runat="server" TextMode="Password"/></td>
<td class="status"></td>
</tr>
<tr>
<td align="right" class="label">Location:</td>
<td  class="field"><asp:TextBox ID="location" runat="server"/></td>
<td class="status"></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnSubmit" Text="Submit" runat="server"/></td>
<td></td>
</tr>
<tr>
<td colspan="3" height="20px">
</td>
</tr>
<tr>
<td></td>
<td colspan="2">
<table>
<tr>
<td>UserName:</td>
<td><asp:Label ID="lbluser" runat="server"/></td>
</tr>
<tr>
<td>Password:</td>
<td><asp:Label ID="lblPwd" runat="server"/></td>
</tr>
<tr>
<td>Location:</td>
<td><asp:Label ID="lblLocation" runat="server"/></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
If you observe above code in header section I added some of script files and css files by using those files we have a chance to show password strength based on text entered in textbox and display the images when validation fails using JQuery in asp.net.

Demo

Download sample code attached