Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Tuesday, July 1, 2008

Javascript : To check the URL Validation







URL:





Saturday, February 23, 2008

JavaScript: Check all option for CheckBox Control.

To check all the Checkboxes in the CheckBox Control:
-----------------------------------------------------
function Allcheck()
{
var ln=document.getElementsByTagName('input');
for(i=1;i {
var input_type=ln[i].type;
if(input_type=="checkbox")
{
var Chck_ID=ln[i].id.indexOf("Check_All");
if(Chck_ID!=-1)
{
ln[i].checked=true;
}
}
}
}

Javscript E-Mail Validation:

Function for Email Validation:-
--------------------------------
function EmailValidation(fld)
{
if(fld.value!='')
{
var id=fld.value;
var Reg=new RegExp("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$");
if(id.match(Reg)==null)
{
alert("Please enter Valid Email-Id !");
fld.value='';
fld.focus();
return false;
}
}
}

Javscript : Function to check Float.

Function to check Float:-
----------------------------
function ChkFloat(fld)
{
if(fld.value!='')
{
var id=fld.value;
var Reg=new RegExp("^[\-]{0,1}[0-9]{1,}(([\.\,]{0,1}[0-9]{1,})|([0-9]{0,}))$");
if(id.match(Reg)==null)
{
alert("Please Enter Numeric Values !");
fld.value='';
fld.focus();
return false;
}
if(id.indexOf("-")!=-1)
{
alert("Please Enter Positive Values !");
fld.value='';
fld.focus();
return false;
}
}
}

Javascript Validation the CheckBox List:

The below code Validate the CheckBox list whether any one of the items are checked or not.

Name of the CheckBox list is : chkFunctionType

function fnValidate()
{
var flag=false;
var chkListfunction = document.getElementById ("chkFunctionType");
var arrCheckbox= chkListfunction.getElementsByTagName("input");
for(var i=0;i {
if(arrCheckbox[i].checked==true)
{
flag=true;
}
}
if(flag==false)
{
alert('Please select the Function Level');
return false;
}
}

Call this Function in the Button Click.

Javascript: Confirmation Msg

function checkdelete()
{
if(confirm('Are you certain that you want to delete this')==false)
{
return false;
}
}

Call the Function in Button OnClick="return checkdelete();"

JavaScript:Allow only Numerics in TextBox

---------------Method 1



In .aspx Page:



--------------Method 2

function validNum(fld)
{
if(fld.value!='')
{
var id=fld.value;
var Reg=new RegExp("^[0-9]*\[0-9]$");
if(id.match(Reg)==null)
{
alert("Please Enter Numeric Values !");
fld.value='';
fld.focus();
return false;
}
if(id.indexOf("-")!=-1)
{
alert("Please Enter Positive Values !");
fld.value='';
fld.focus();
return false;
}
}
}

In .aspx.cs Page:

Text Box Name: txtCorpName
txtCorpName.Attributes.Add("OnKeyup", "Javascript:return(validNum(this));");

onbeforeunload Javascript

There are some cases where you might want to instruct the user before he/She is navigating to the next page. That might be a meaningful message like "Are you sure you want to navigate away from the current page?"
Probably end user pressed the X button by mistake and he/she might loose his/her current state

There is onbeforeunload event for the JavaScript that fires when you are navigating away from the current page.
Here is the Code for that.



script type="text/javascript"
function close()
{
event.returnValue = "This will navigate to next page.";
}
script




body onbeforeunload="close()"



Note:
The default statement that appears in the dialog box, "Are you sure you want to navigate away from this page? ... Press OK to continue, or Cancel to stay on the current page.", cannot be removed or altered.

Maxlength checking in Textarea Using Javascript

function maxcheck()
{
var maxval=1000;
var maxtxtlength=window.document.myform.Description.value.length;
if(parseInt(maxtxtlength)>parseInt(maxval))
{
alert("This Description field accept only 1000 characters");
window.document.myform.Description.innerText=window.document.myform.Description.value.substring(0,1000);
return false;
}
}

Accesing Parent Window through Childwindow using Javascript

///////////////////////Accessing Id///////////////////////////
alert(window.opener.document.getElementById("HidString").value)

///////////////////////End Accessing Id///////////////////////////

///////////////////////Accessing Function///////////////////////////
child.aspx
window.opener.LoadEmp(obj)

parent.aspx
function LoadEmp(ob)
{
alert(ob);
}

///////////////////////End Accessing Function///////////////////////////


///////////////////////Refershing Parent Page from Child page////////////

Response.Write("");

Reading and removing Listbox using Javascript

/////////////Reading/////////////////////////////////
var listEmp=document.getElementById("PMEmpname");

for(j=0;j
{
listEmp.options[j].value //Value field
listEmp.options[j].text //Text Field
}

///////////Deleting///////////////////////////////////

for(j=0;j{
listEmp.remove(j);
}

Using Javascript creating control without postback




















Friend Name

Email Id

Delete






/////////////////////////////////Javascript///////////////////////////
///////////////////This is for adding row/////////////////////////////
var rowcount=1;
var checkcount=1;
function fnAddRow()
{
rowcount++;
// var check=validate(); //this is for my page validation
if(check!=false)
{
var objTbl = document.getElementById("Tellafriend1");
var objTbody = objTbl.getElementsByTagName("tbody")[0];
var row = document.createElement("tr");
//txtfriendname///
var friendnameval=document.createElement("td");
var friendnameDel = document.createElement("INPUT")
friendnameDel.setAttribute("type","text");
friendnameDel.setAttribute("name","txtfriendname"+rowcount);
friendnameDel.setAttribute("id","txtfriendname"+rowcount);
friendnameval.appendChild(friendnameDel)
row.appendChild(friendnameval);
//txtfriendname///
//txtfriendemail///
var friendemailval=document.createElement("td");
var friendemailDel = document.createElement("INPUT")
friendemailDel.setAttribute("type","text");
friendemailDel.setAttribute("name","txtfriendemail"+rowcount);
friendemailDel.setAttribute("id","txtfriendemail"+rowcount);
friendemailval.appendChild(friendemailDel)
row.appendChild(friendemailval);
//txtfriendemail///

//checkbox///
var checkval=document.createElement("td");
var ChkDel = document.createElement("INPUT")
ChkDel.setAttribute("type","checkbox");
ChkDel.setAttribute("name","chkDelete"+rowcount);
ChkDel.setAttribute("id","chkDelete"+rowcount);
checkval.appendChild(ChkDel)
row.appendChild(checkval);
//End check box checkbox///
objTbody.appendChild(row);
}
else
{
return false;
}
}
///////////////////End adding row/////////////////////////////

Note:For deleting i am using checkbox...................
///////////////Deleting Row//////////////////////////////////

function fnDelRow()
{
var ChkOption = ""
for(var i=2;i<=rowcount;i++)
{
var ChkCtrl = document.getElementById("chkDelete"+i)

if(ChkCtrl == '[object]')
{
if(ChkCtrl.checked)
{
ChkOption = "1"
delRow(ChkCtrl)
checkcount++;
}
}
}
if(ChkOption == "" && rowcount!=1)
{
alert("Select any one option to delete")
return false;
}
}
function delRow(button)
{
var row = button.parentNode.parentNode;
var tbody = document.getElementById('Tellafriend1').getElementsByTagName('tbody')[0];
tbody.removeChild(row);
}

/////////////////////////////End Deleting row////////////////////////////////

Javascript Debugger Enabling

Step 1. Disable Script Debugger - Make this option unchecked in the IE.
function someFunct()
{
Javascript:debugger;alert(window.name)
alert("Mahesh");
}

How to Set Screen width for any kind of resolution?



2) Call the function on load event of body.