Search This Blog

Thursday, March 31, 2011

JavaScript Validations

Default.aspx Page Start
<%@ 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">
<title>Registration Form</title>
<script language="javascript" type="text/javascript">
function fnValidate()
{
//FName Validation.
fnValidateFirstName();
//LName Validation
fnValidateLastName();
//Mobile validation
fnValidateMobileNumber();
//PEmail validation
fnValidatePrimaryEmail();
//SEmail Validation
fnValidateSecondaryEmail();
//Counttry Validation
fnValidateCountries();
}
function fnValidateSecondaryEmail()
{
var semail=document.getElementById("txtsemail").value;
var semailregex=/^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
if(!semail.match(semailregex))
{
alert("enter valid emailid");
}
}
function fnValidatePrimaryEmail()
{
var pemailno=document.getElementById("txtPemail").value;
if(pemailno=="")
{
alert("email id is mandatory");
}
var pemailregex=/^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
if(!pemailno.match(pemailregex))
{
alert("enter valid emailid");
}
}
function fnValidateMobileNumber()
{
var mobile=document.getElementById("txtMobile").value;
if(mobile=="")
{
alert("Mobile number is mandatory");
}
var mno= /^((\+)?(\d{2}[-]))?(\d{10}){1}?$/;
if(!mobile.match(mno))
{
alert("1.Mobile Number should Have Only Digits .\n2.Mobile Number should contain 10 digits");
return false;
}
else
{
return true;
}
}
function fnValidateLastName()
{
var lname=document.getElementById("txtlname").value;
var alphaExp= /^[a-zA-Z]+$/;
if(!lname.match(alphaExp))
{
alert("Last name should have only alphabets");
return false;
}
else
{
return true;
}
}
function fnValidateFirstName()
{
var fname=document.getElementById("txtfname").value;
if(fname=="")
{
alert("First Name Cannot be Blank.");
return false;
}
else
{
var alphaExp = /^[a-zA-Z]+$/;

if(!fname.match(alphaExp))
{
alert("First Name should contain Only Alphabets.");
return false;
}
return true;
}
}
function fnValidateCountries()
{
var index=document.getElementById("ddlCountry").selectedIndex;
alert(index);
if(index==0)
{
alert("Please Select a Country.");
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="100%">
<tr>
<td width="15%"></td>
<td width="70%">
<table>
<tr>
<td align="right">FirstName:</td>
<td><asp:TextBox ID="txtfname" runat="server" /></td>
<td align="right">Mobile:</td>
<td><asp:TextBox ID="txtMobile" runat="server" /></td>
</tr>
<tr>
<td align="right">LastName:</td>
<td><asp:TextBox ID="txtlname" runat="server" /></td>
<td align="right">Phone:</td>
<td><asp:TextBox Width="10%" ID="tbCountryCode" runat="server" />

<asp:TextBox Width="10%" ID="tbCityCode" runat="server" />

<asp:TextBox Width="29%" ID="tbPhone" runat="server" />
</td>
</tr>
<tr>
<td align="right">PrimaryMail: </td>
<td>
<asp:TextBox ID="txtPemail" runat="server" />
</td>
<td align="right">SecondaryMail:</td>
<td>
<asp:TextBox ID="txtsemail" runat="server" />
</td>
</tr>
<tr>
<td align="right">Address1:</td>
<td>
<asp:TextBox ID="txtaddress1" runat="server" TextMode="MultiLine" rows="5"/>
</td>
<td></td>
<td></td>
</tr>
<tr>
<td>Country:</td>
<td><asp:DropDownList ID="ddlCountry" runat="server"
width="80%"></asp:DropDownList></td>
<td></td>
<td><asp:Button ID="btnSubmit" runat="server" Text="Submit"
OnClientClick="return fnValidate()" /></td>
</tr>
</table>
</td>
<td width="15%"></td>
</tr>
</table>
</div>
</form>
</body>
</html>
Default.aspx Page End


Default.aspx.cs Page Start

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillCountries();
        }
    }

    private void FillCountries()
    {
        ArrayList alCountries = new ArrayList();
        alCountries.Add("India");
        alCountries.Add("Pakistan");
        alCountries.Add("Srilanka");
        alCountries.Add("Australia");
        ddlCountry.DataSource = alCountries;
        ddlCountry.DataBind();
        ddlCountry.Items.Insert(0, "--Select--");
    }
}


Default.aspx.cs Page End

Thursday, March 17, 2011

What is JavaScript


  • JavaScript is a Scripting language , The JavaScript(js) code will be executed by the JavaScript engine which comes with the Browser ( I.E,Opera,Mozilla etc..)
  • JavaScript is a loosely typed language and is called client side scripting language. Because the js code you have written in the .aspx  pages will be sent back to client browser by the web server (IIS) and the JavaScript code will be executed by Browser hence we call this as Client side Script.
  • The Html and js code sent by IIS to browser will be executed by HTML parser and JavaScript engine which are present in the Browser software ( Browser like IE,Opera, Chrome all are softwares which contains code written in c-language etc..)
  • Note: For .net training institutes in Bangalore you can always visit http://techpalle.com/net-training-in-bangalore.html