Validation in server side
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="myprofile.aspx.cs" Inherits="myprofile" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script>
function OnlyNumbers(evt) {
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode != 46 && charCode > 31
&& (charCode < 48 || charCode > 57))
return false;
return true;
}
function checkEmail(event) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (!re.test(event.value)) {
alert("Please enter a valid email address");
return false;
}
return true;
}
function chkvalidation()
{
var exitval = false;
var rsltcount = 0;
var msgtext = "";
//Age Validation
var ageval = document.getElementById("<%= txtAge.ClientID %>").value;
var agerslt = false;
if (ageval.length > 0)
{
agerslt = OnlyNumbers(document.getElementById("<%= txtAge.ClientID %>"));
if (agerslt == false)
{
msgtext = "Invalid age entered.";
rsltcount = 1
}
else
{
if (ageval > 100) {
msgtext = msgtext + "\n Check your entered age.";
rsltcount += 1
}
}
}
else
{
msgtext = msgtext + "\n Invalid age or age required.";
rsltcount += 1
}
//Mobile Validation
var mobileval = document.getElementById("<%= txtMobile.ClientID %>").value;
var mobilerslt = false;
if (mobileval.length != 10) {
msgtext = msgtext + "\n Invalid mobile number or mobile number required";
rsltcount += 1
}
else {
mobilerslt = OnlyNumbers(document.getElementById("<%= txtMobile.ClientID %>"));
if (mobilerslt == false) {
msgtext = msgtext + "\n Invalid mobile number or mobile number required.";
rsltcount += 1
}
}
//Email Validation
var emailidval = document.getElementById("<%= txtEmailID.ClientID %>").value;
var emailidrslt = false;
if (emailidval.length > 1) {
emailidrslt = checkEmail(document.getElementById("<%= txtEmailID.ClientID %>"));
if (emailidrslt == false) {
msgtext = msgtext + "\n Invalid email id entered.";
rsltcount += 1
}
}
else {
msgtext = msgtext + "\n Email id required.";
rsltcount += 1
}
if (rsltcount > 0)
{
exitval = false;
}
else
{
exitval = true;
}
alert(msgtext);
return exitval;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Name
</td>
<td>
<asp:TextBox ID="txtName" runat="server" ClientIDMode="Static"></asp:TextBox>
</td>
</tr>
<tr>
<td>Age
</td>
<td>
<asp:TextBox ID="txtAge" runat="server" onkeypress="return OnlyNumbers(event)" MaxLength="3" ></asp:TextBox>
</td>
</tr>
<tr>
<td>Mobile
</td>
<td>
<asp:TextBox ID="txtMobile" runat="server" MaxLength="10" onkeypress="return OnlyNumbers(event)" ></asp:TextBox>
</td>
</tr>
<tr>
<td>Email ID
</td>
<td>
<asp:TextBox ID="txtEmailID" runat="server" placeholder="yourname@server.com" onblur="checkEmail(this)"></asp:TextBox>
</td>
</tr>
<tr>
<td rowspan="1">
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="btnSubmit_Click" OnClientClick="return chkvalidation()" />
</td>
</tr>
</table>
<asp:Label ID="lblResultMessage" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class myprofile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string MsgText = "";
Int32 rsltcount = 0;
//Age Validation
bool ageValStatus = VerifyNumericValue(txtAge.Text);
if (ageValStatus == false)
{
rsltcount += 1;
MsgText += "Invalid age or age required.</br>";
}
if (ageValStatus == true)
{
if(Convert.ToDecimal(txtAge.Text) > 100)
{
rsltcount += 1;
MsgText += " Check your entered age.</br>";
}
}
//Mobile Validation
bool mobileValStatus = VerifyNumericValue(txtMobile.Text);
if (mobileValStatus == false)
{
rsltcount += 1;
MsgText += "Invalid mobile number or mobile number required.</br>";
}
if (mobileValStatus == true)
{
if (txtMobile.Text.Length != 10)
{
rsltcount += 1;
MsgText += "Check your entered mobile number.</br>";
}
}
//Email ID Validation
bool emailidValStatus = VerifyEmailID(txtEmailID.Text);
if (emailidValStatus == false)
{
rsltcount += 1;
MsgText += "Invalid email id or email id required.</br>";
}
lblResultMessage.Text = MsgText;
lblResultMessage.Font.Bold = false;
lblResultMessage.Font.Size = 14;
lblResultMessage.ForeColor = System.Drawing.Color.Red;
}
public bool VerifyNumericValue(string ValueToCheck)
{
Int32 numval;
bool rslt = false;
rslt = Int32.TryParse(ValueToCheck, out numval);
return rslt;
}
public static bool VerifyEmailID(string email)
{
string expresion;
expresion = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
if (Regex.IsMatch(email, expresion))
{
if (Regex.Replace(email, expresion, string.Empty).Length == 0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
GridView bind
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<table style="width:100%;">
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"
CellPadding="4" DataKeyNames="id">
<Columns>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("city") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("city") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<SortedAscendingCellStyle BackColor="#EDF6F6" />
<SortedAscendingHeaderStyle BackColor="#0D4AC4" />
<SortedDescendingCellStyle BackColor="#D6DFDF" />
<SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
<div>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
refreshdata();
}
}
public void refreshdata()
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
SqlCommand cmd = new SqlCommand("select * from tbl_data", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}