Monday, 2 April 2012
Sunday, 1 April 2012
Time Validation using JavaScript
<!-- TWO STEPS TO INSTALL TIME VALIDATION:
1. Copy the coding into the HEAD of your HTML document
2. Add the last code into the BODY of your HTML document -->
<!-- STEP ONE: Paste this code into the HEAD of your HTML document -->
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Sandeep Tamhankar (stamhankar@hotmail.com) -->
<!-- Web Site: http://207.20.242.93 -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://www.javascriptsource.com -->
<!-- Begin
function IsValidTime(timeStr) {
// Checks if time is in HH:MM:SS AM/PM format.
// The seconds and AM/PM are optional.
var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
var matchArray = timeStr.match(timePat);
if (matchArray == null) {
alert("Time is not in a valid format.");
return false;
}
hour = matchArray[1];
minute = matchArray[2];
second = matchArray[4];
ampm = matchArray[6];
if (second=="") { second = null; }
if (ampm=="") { ampm = null }
if (hour < 0 || hour > 23) {
alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
return false;
}
if (hour <= 12 && ampm == null) {
if (confirm("Please indicate which time format you are using. OK = Standard Time, CANCEL = Military Time")) {
alert("You must specify AM or PM.");
return false;
}
}
if (hour > 12 && ampm != null) {
alert("You can't specify AM or PM for military time.");
return false;
}
if (minute<0 || minute > 59) {
alert ("Minute must be between 0 and 59.");
return false;
}
if (second != null && (second < 0 || second > 59)) {
alert ("Second must be between 0 and 59.");
return false;
}
return false;
}
// End -->
</script>
</HEAD>
<!-- STEP TWO: Copy this code into the BODY of your HTML document -->
<BODY>
<center>
<form name=timeform onSubmit="return IsValidTime(document.timeform.time.value);">
Time (HH:MM:SS AM/PM format) <input type=text name=time><br>
<input type="submit" value="Submit">
</form>
</center>
<p><center>
<font face="arial, helvetica" size="-2">Free JavaScripts provided<br/>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>
<!-- Script Size: 1.92 KB -->
Monday, 26 March 2012
Client Side Validation of GridView control with javascript
Client Side Validation Of GridView with JavaScript.
Many
people are asking me how to validate GridView with JavaScript. How to
get control client Id on client side. This article and attached code
snippet shows you to create a validate method in JavaScript and call it
from your ASP.NET page's code behind to validate the controls.
In my sample example, I have taken on GridView with checkbox and TextBox in
the gridview. Just copy this code in your aspx page. As you can see in
the below code, I have a validate method. Thid methods finds each
control in a GridView row using getElementById as an object and then
checks if it is checked or not and so on.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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">
<script type="text/javascript">
function validate()
{
for(j=0;j<grd_Cb.length;j++)
{
var obj = document.getElementById(grd_Cb[j]);
if(obj.checked ==true)
{
Checkbol=1;
bool=1;
}
}
if(bool==0)
{
alert(" Please enter atleast one Passenger");
return false;
}
if (Checkbol==1)
{
for(i=0;i<grd_Cb.length;i++)
{
var Obj1 = document.getElementById(grd_Cb[i]);
if(Obj1.checked ==true)
{
var objFirstName=document.getElementById(grdFirstName_Txt[i]);
if(objFirstName.value=="")
{
alert("Line No : "+ [parseInt(i)+1]+ " First name can not be blank");
objFirstName.focus();
return false;
}
var objLastName=document.getElementById(grdLastName_Txt[i]);
if(objLastName.value=="")
{
alert("Line No : "+ [parseInt(i)+1]+ " Last name can not be blank");
objLastName.focus();
return false;
}
}
}
}
return true;
}
</script>
<title>JavaScript Validation on Grid</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView runat="server" ID="JavascriptGrid" AutoGenerateColumns="False" Width="100%" OnPreRender="JavascriptGrid_PreRender">
<Columns>
<asp:TemplateField HeaderText="Validate">
<ItemTemplate>
<asp:CheckBox CssClass="textbox" runat="server" ID="ChkValidate" />
</ItemTemplate>
<ItemStyle Width="10px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:TextBox ID="FirstName" Text='<%#Eval("FirstName") %>' runat="server" Width="140"></asp:TextBox>
</ItemTemplate>
<ItemStyle Width="150px" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:TextBox ID="LastName" Text='<%#Eval("LastName") %>' runat="server" Width="140"></asp:TextBox>
</ItemTemplate>
<ItemStyle Width="140px" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:TextBox ID="Email" Text='<%#Eval("Email") %>' runat="server" Width="140"></asp:TextBox>
</ItemTemplate>
<ItemStyle Width="140px" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Zip">
<ItemTemplate>
<asp:TextBox ID="Zip" Text='<%#Eval("Zip") %>' runat="server" Width="50"></asp:TextBox>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnValidate" runat="server" Text="ValidateGrid" />
</form>
</body>
</html>
And
following code in .cs file of the same page. As you can see on this
page, on page's Load event handler, I add an attribute to the button's
click event hander onclick and call JavaScript's validate method
whenever a button is clicked.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
btnValidate.Attributes.Add("onclick", "return validate()");
DataTable dt = new DataTable();
DataRow dtRow;
dt.Columns.Add("FirstName");
dt.Columns.Add("LastName");
dt.Columns.Add("Email");
dt.Columns.Add("Zip");
for (int i = 0; i < 4; i++)
{
dtRow = dt.NewRow();
dt.Rows.Add(dtRow);
}
JavascriptGrid.DataSource = dt;
JavascriptGrid.DataBind();
}
protected void JavascriptGrid_PreRender(object sender, EventArgs e)
{
ClientScriptManager cs = Page.ClientScript;
foreach (GridViewRow grdrow in JavascriptGrid.Rows)
{
CheckBox txtgrdValidate = (CheckBox)grdrow.FindControl("ChkValidate");
TextBox txtgrdFirstName = (TextBox)grdrow.FindControl("FirstName");
TextBox txtgrdLastName = (TextBox)grdrow.FindControl("LastName");
TextBox txtgrdEmail = (TextBox)grdrow.FindControl("Email");
TextBox txtgrdZip = (TextBox)grdrow.FindControl("Zip");
cs.RegisterArrayDeclaration("grd_Cb", String.Concat("'", txtgrdValidate.ClientID, "'"));
cs.RegisterArrayDeclaration("grdFirstName_Txt", String.Concat("'", txtgrdFirstName.ClientID, "'"));
cs.RegisterArrayDeclaration("grdLastName_Txt", String.Concat("'", txtgrdLastName.ClientID, "'"));
cs.RegisterArrayDeclaration("grdEmail_Txt", String.Concat("'", txtgrdEmail.ClientID, "'"));
cs.RegisterArrayDeclaration("grdZip_Txt", String.Concat("'", txtgrdZip.ClientID, "'"));
}
}
}
Using the same approach, you may apply client side validation on any other controls or events.
Monday, 27 February 2012
Singleton Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BusinessLayer
{
public partial class Management
{
#region "Code to follow Singleton Design Pattern"
/// <summary>
/// Declare a private static variable
/// </summary>
private static Management _Instance;
/// <summary>
/// Return the instance of the application by initialising once only.
/// </summary>
public static Management GetInstance
{
get
{
if (_Instance == null)
{
_Instance = new Management();
}
return _Instance;
}
set
{
_Instance = value;
}
}
#endregion
//public List<Company> GetChildCompany(int p)
//{
// throw new NotImplementedException();
//}
public void InsertVoucher(Objects.PaymentReceipt newVoucher)
{
throw new NotImplementedException();
}
}
}
Friday, 24 February 2012
Return value from storedprocedure
Stored Procedure:-
ALTER PROCEDURE TestInsert
(
@RollNo int,
@Name nvarchar(50),
@Gender varchar(10),
@Age int,
@DOB nvarchar(50),
@Address nvarchar(100),
@ContactNo nvarchar(15),
@Status int,
@Uimage nvarchar(100),
@Result int output
)
as
Begin
set @Result=0
if(exists(select RollNo from RegForm where RollNo=@RollNo))
Begin
update RegForm set Name=@Name,Gender=@Gender,Age=@Age,DOB=convert(datetime,@DOB,103),
Address=@Address,ContactNo=@ContactNo,Status=@Status,Uimage=@Uimage where RollNo=@RollNo
set @Result=1
end
else
Begin
insert into RegForm (RollNo,Name,Gender,Age,DOB,Address,ContactNo,Status,Uimage)
values
(@RollNo,@Name,@Gender,@Age,convert(datetime,@DOB,103),@Address,@ContactNo,@Status,@Uimage)
set @Result=2
end
end
C# Code:-
public int InsertRecord(ObjectLayer.ObjectLayer objobject)
{
int result = 0;
try
{
SqlCommand com = new SqlCommand("TestInsert", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@RollNo", objobject.RollNo);
com.Parameters.AddWithValue("@Name", objobject.Name);
com.Parameters.AddWithValue("@Gender", objobject.Gender);
com.Parameters.AddWithValue("@Age", objobject.Age);
com.Parameters.AddWithValue("@DOB", objobject.DOB);
com.Parameters.AddWithValue("@Address", objobject.Address);
com.Parameters.AddWithValue("@ContactNo", objobject.ContactNo);
com.Parameters.AddWithValue("@Status", objobject.Stat);
com.Parameters.AddWithValue("@Uimage", objobject.Uimage);
SqlParameter pa1 = new SqlParameter("@Result", SqlDbType.Int);
pa1.Direction = ParameterDirection.Output;
pa1.Size = 100;
com.Parameters.Add(pa1);
ExecuteStoredProcedure(com);
result = Convert.ToInt32(com.Parameters["@Result"].Value.ToString());
}
catch (Exception ex)
{
string s = ex.Message.ToString();
}
return result;
}
ALTER PROCEDURE TestInsert
(
@RollNo int,
@Name nvarchar(50),
@Gender varchar(10),
@Age int,
@DOB nvarchar(50),
@Address nvarchar(100),
@ContactNo nvarchar(15),
@Status int,
@Uimage nvarchar(100),
@Result int output
)
as
Begin
set @Result=0
if(exists(select RollNo from RegForm where RollNo=@RollNo))
Begin
update RegForm set Name=@Name,Gender=@Gender,Age=@Age,DOB=convert(datetime,@DOB,103),
Address=@Address,ContactNo=@ContactNo,Status=@Status,Uimage=@Uimage where RollNo=@RollNo
set @Result=1
end
else
Begin
insert into RegForm (RollNo,Name,Gender,Age,DOB,Address,ContactNo,Status,Uimage)
values
(@RollNo,@Name,@Gender,@Age,convert(datetime,@DOB,103),@Address,@ContactNo,@Status,@Uimage)
set @Result=2
end
end
C# Code:-
public int InsertRecord(ObjectLayer.ObjectLayer objobject)
{
int result = 0;
try
{
SqlCommand com = new SqlCommand("TestInsert", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@RollNo", objobject.RollNo);
com.Parameters.AddWithValue("@Name", objobject.Name);
com.Parameters.AddWithValue("@Gender", objobject.Gender);
com.Parameters.AddWithValue("@Age", objobject.Age);
com.Parameters.AddWithValue("@DOB", objobject.DOB);
com.Parameters.AddWithValue("@Address", objobject.Address);
com.Parameters.AddWithValue("@ContactNo", objobject.ContactNo);
com.Parameters.AddWithValue("@Status", objobject.Stat);
com.Parameters.AddWithValue("@Uimage", objobject.Uimage);
SqlParameter pa1 = new SqlParameter("@Result", SqlDbType.Int);
pa1.Direction = ParameterDirection.Output;
pa1.Size = 100;
com.Parameters.Add(pa1);
ExecuteStoredProcedure(com);
result = Convert.ToInt32(com.Parameters["@Result"].Value.ToString());
}
catch (Exception ex)
{
string s = ex.Message.ToString();
}
return result;
}
Subscribe to:
Posts (Atom)