9 September 2010

Java script validations

Java script validations
<--script type ="text/javascript">

//Validating the user inpus in the form
function Validate() {
if (document.getElementById("<%=txtName.ClientID%>").value == "")
{
alert("Name is a mandatory field and it can not be empty");
document.getElementById("<%=txtName.ClientID%>").focus();
return false;
}
else if (document.getElementById("<%=txtAddress.ClientID%>").value == "")
{
alert("Address is a Mandatory field and it can not be empty");
document.getElementById("<%=txtAddress.ClientID%>").focus();
return false;
}
else if (document.getElementById("<%=txtLandMark.ClientID%>").value == "")
{
alert("LandMark is Mandatory field and it can not be empty");
document.getElementById("<%=txtLandMark.ClientID%>").focus();
return false;
}
else if (document.getElementById("<%=txtDOB.ClientID%>").value == "")
{
alert("Date of Birth is a Mandatory field and it can not be empty");
document.getElementById("<%=txtDOB.ClientID%>").focus();
return false;
}
else if (document.getElementById("<%=ddlTitle.ClientID%>").value == "-select-")
{
alert("please select the title field");
document.getElementById("<%=ddlTitle.ClientID%>").focus();
return false;
}

}

/* address validation function */
function addressCheck()
{

var msg = " Address field can accept only \n alpha numerics \n white spaces and the following special characters \n";
msg = msg+ " . / # - ,";
var a = 1;
var iChars = "~`!@$%^&*()+=[]\';{}|\":<>?";
var txtval = '';
var strField = document.getElementById("<%=txtAddress.ClientID%>").value;
for (var i = 0; i < strField.length; i++)
{
if (iChars.indexOf(strField.charAt(i)) != -1)
{
a = 1;
break;
}
else
a = 2;
}

if (a == 1)
{
alert(msg);
document.getElementById("<%=txtAddress.ClientID%>").focus();
return false;
}
else if (a == 2)
{
return true;
}

}

/* land mark validation function */
function landMarkCheck()
{
var msg = " LandMark field can accept only \n alpha numerics \n white spaces and the following special characters \n";
msg = msg + " . ,";
var a = 1;
var iChars = "~`!#@$%^&*()+=-[]\\\';/{}|\":<>?";
var txtval = '';
var strField = document.getElementById("<%=txtLandMark.ClientID%>").value;
for (var i = 0; i < strField.length; i++) {
if (iChars.indexOf(strField.charAt(i)) != -1)
{
a = 1;
break;
}
else
a = 2;
}

if (a == 1)
{
alert(msg);
document.getElementById("<%=txtLandMark.ClientID%>").focus();
return false;
}
else if (a == 2) {
return true;
}
}
/* date validation */
function dateCheck() {
var s = document.getElementById("<%=txtDOB.ClientID%>").value;
var y = s.substring(6, 10);
var d = s.substring(0, 2);
var m = s.substring(3, 5);
var cdate = new Date();
var yy = cdate.getFullYear() - 1;
//checking the date is between 1910 to current date
if ((y >= 1900) && (y <= yy))
{
return true;
}
else if(y==yy+1)
{
if (m <= cdate.getMonth() + 1)
{
if (m == cdate.getMonth() + 1)
{
if (d <= cdate.getDate())
{
if (d == cdate.getDate());
return true;
}
else {
alert("Date of birth should be from 1910 to current date and also in the specified format ");
document.getElementById("<%=txtDOB.ClientID%>").focus();
return false;
}
}

}
else {
alert("Date of birth should be from 1910 to current date and also in the specified format");
document.getElementById("<%=txtDOB.ClientID%>").focus();
return false;

}
}
else {
alert("Date of birth should be from 1910 to current date and also in the specified format");
document.getElementById("<%=txtDOB.ClientID%>").focus();
return false;
}


}

<--script type="text/javascript">




1. write scripts with in tag
2. call the functions from code behind using script registration.

example:

page scripts of : add or edit page



----------------------------------------------------------------------------------------------------------

script registration in code behind for add or edit page

protected void Page_Load(object sender, EventArgs e)
{
//checking page is
if (!Page.IsPostBack)
{
//here we can add the jscript functions to the required control's events
btnSubmit.Attributes.Add("OnClick", "return Validate();");
txtAddress.Attributes.Add("onblur", "return addressCheck();");
txtLandMark.Attributes.Add("onblur", "return landMarkCheck();");
txtDOB.Attributes.Add("onblur", "return dateCheck();");
}
}

---------------------------------------------------------------------------------------------------------
page script for : modify page



---------------------------------------------------------------------------------------------------------

script registration in code behind for modify page


if (!Page.IsPostBack)
{
if (gvTestDetails.Rows.Count != 0)
{ btnDelete.Attributes.Add("OnClick", "return pop();"); }
else
{
btnDelete.Attributes.Add("OnClick", "return pop1();");
btnEdit.Attributes.Add("OnClick", "return pop2();");

}
}

No comments:

Post a Comment

Comments Welcome

Consistency level in Azure cosmos db

 Consistency level in Azure cosmos db Azure Cosmos DB offers five well-defined consistency levels to provide developers with the flexibility...