Library code snippets
Some Javascript General Functions
By Hari K, published on 03 Jul 2007
General Functions
//Accepts Only AlphaCharacters
function AlphaOnly()
{
var Key = window.event.keyCode;
if((Key >= 65 && Key <= 90) || (Key >= 97 && Key <= 122)||Key==32)
return true;
return false;
}
//Accepts Only Numeric and Dot
function NumericDotOnly()
{
var Key = window.event.keyCode;
if(( Key >= 48 && Key <= 57) || Key==46 )
return true;
return false;
}
//Accepts Only Numeric and Colon
function NumericColonOnly()
{
var Key = window.event.keyCode;
if( Key >= 48 && Key <= 57 || Key==58)
return true;
return false;
}
//Allow Numeric and Minus Sign
function fncNumericMinus()
{
var Key = window.event.keyCode;
if(( Key >= 48 && Key <= 57) || Key==46 || Key==45)
return true;
return false;
}
//Accepts Only Numeric
function NumericOnly()
{
var Key = window.event.keyCode;
if( Key >= 48 && Key <= 57)
return true;
return false;
}
//Allow + and Number Only
function MobileOnly()
{
var Key = window.event.keyCode;
if(Key >= 48 && Key <= 57 || Key == 43)
return true;
return false;
}
function fncNullchk(name,col,val)
{
var j=0;
len = trim(val).length;
for(i=0;i<len;i++)
{
if (val.substr(i,1) != " ")
j++;
}
if (j == 0)
{
if (!col.disabled)
{
alert("Please Enter the " + name);
col.focus();
}
return false;
}
if (val == 0)
{
alert("Please Enter the Value for " + name);
col.focus();
return false;
}
return true;
}
//..........................................................................................
// FOR E-MAIL CHECK */
// Eg. emailchk(document.form.text1,document.form.text1.value) */
//..........................................................................................
function fncEmailValid(col,val)
{
var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
var matchArray=val.match(emailPat)
if (matchArray==null)
{
alert("E-mail address is incorrect");
col.focus();
return false;
}
var user=matchArray[1]
var domain=matchArray[2]
if (user.match(userPat)==null)
{
alert("The E-mail username doesn't seem to be valid.");
return false;
}
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null)
{
for (var i=1;i<=4;i++)
{
if (IPArray[i]>255)
{
alert("Destination IP address is invalid!")
col.focus();
return false
}
}
return true
}
var domainArray=domain.match(domainPat)
if (domainArray==null)
{
alert("The domain name doesn't seem to be valid.")
col.focus();
return false
}
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 )
{
alert("The address must end in a three-letter domain, or two letter country.")
col.focus();
return false
}
if (len<2)
{
var errStr="This address is missing a hostname!"
alert(errStr);
col.focus();
return false;
}
return true;
}
//..........................................................................................
//CHECK THE WHETHER FROMDATE IS LESSER THAN TODATE OR BOTH DATES CAN BE EQUAL
//..........................................................................................
function fncCompareDates(from, to)
{
var strDay,strMonth,strYear,fromdate,todate;
var intfromdate,inttodate;
fromdate = from;
todate = to;
strDay = fromdate.substr(0,2);
strMonth = fromdate.substr(3,2);
strYear = fromdate.substr(6,4);
fromdate = strYear+strMonth+strDay;
strDay = todate.substr(0,2);
strMonth = todate.substr(3,2);
strYear = todate.substr(6,4);
todate = strYear+strMonth+strDay;
intfromdate=parseInt(fromdate, 10);
inttodate=parseInt(todate, 10);
if (intfromdate <= inttodate)
{
return true;
}
else
{
if (from.value == "" || to.value == "")
{
alert("Both the dates must be entered");
return false;
}
else
{
return false;
}
}
}
//CHECK ALL THE CHECKBOX DEPEND ON THE STATE OF CHECKBOX SELECTION AT THE TOP
function fncCheckAllDGrid(aspCheckBoxID, checkVal)
{
re = new RegExp(':' + aspCheckBoxID + '$');
//generated controlname starts with a colon
for(i = 0; i < document.forms[0].elements.length; i++)
{
elm = document.forms[0].elements[i]
if ((elm.type == 'checkbox') && !(elm.disabled))
{
if (re.test(elm.name))
{
elm.checked = checkVal
}
}
}
}
function fncUnCheckHeader(PGridId,aspCheckBoxHeaderID,checkStatus)
{
strCBMid=PGridId+'__ctl1_'+aspCheckBoxHeaderID
if (checkStatus==false)
{
document.all(strCBMid).checked=false;
}
}
//....................................................................................
// CHECKING WHETHER TIME IS VALID OR NOT
//10:50...............................................................................
function fncIsTimeValid(strTime)
{
if (strTime.length != 5)
{
return false;
}
var iHour = parseInt(strTime.substring(0,2))
var iColon = strTime.substring(2,3)
var iMinute = parseInt(strTime.substring(3,5))
if (iColon != ":")
{
return false;
}
if ((iHour < 0) || (iHour > 23))
{
return false;
}
if ((iMinute < 0) || (iMinute > 59))
{
return false;
}
return true;
}
//CHECKING WHETHER CHAR IS APPEARED MORE THAN ONCE IN VALUE STRING
function IsCharExist(value,Char)
{
var status=new String(value);
var len = status.length;
var lCPeriodPosition = -1;
//CHECK CHAR IS IN THE STRING
if (status.indexOf(Char,0)>=0)
{
for(var i=0;i<len;i++)
{
if(status.charAt(i)==Char)
{
lCPeriodPosition++;
}
}
}
if (status.indexOf(Char,0)>=0)
{
if (lCPeriodPosition>0)
{
return false;
}
}
if (status.indexOf(Char)>=-1)
{
return true;
}
}
function AlphaOnly()
{
var Key = window.event.keyCode;
if((Key >= 65 && Key <= 90) || (Key >= 97 && Key <= 122)||Key==32)
return true;
return false;
}
//Accepts Only Numeric and Dot
function NumericDotOnly()
{
var Key = window.event.keyCode;
if(( Key >= 48 && Key <= 57) || Key==46 )
return true;
return false;
}
//Accepts Only Numeric and Colon
function NumericColonOnly()
{
var Key = window.event.keyCode;
if( Key >= 48 && Key <= 57 || Key==58)
return true;
return false;
}
//Allow Numeric and Minus Sign
function fncNumericMinus()
{
var Key = window.event.keyCode;
if(( Key >= 48 && Key <= 57) || Key==46 || Key==45)
return true;
return false;
}
//Accepts Only Numeric
function NumericOnly()
{
var Key = window.event.keyCode;
if( Key >= 48 && Key <= 57)
return true;
return false;
}
//Allow + and Number Only
function MobileOnly()
{
var Key = window.event.keyCode;
if(Key >= 48 && Key <= 57 || Key == 43)
return true;
return false;
}
function fncNullchk(name,col,val)
{
var j=0;
len = trim(val).length;
for(i=0;i<len;i++)
{
if (val.substr(i,1) != " ")
j++;
}
if (j == 0)
{
if (!col.disabled)
{
alert("Please Enter the " + name);
col.focus();
}
return false;
}
if (val == 0)
{
alert("Please Enter the Value for " + name);
col.focus();
return false;
}
return true;
}
//..........................................................................................
// FOR E-MAIL CHECK */
// Eg. emailchk(document.form.text1,document.form.text1.value) */
//..........................................................................................
function fncEmailValid(col,val)
{
var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
var matchArray=val.match(emailPat)
if (matchArray==null)
{
alert("E-mail address is incorrect");
col.focus();
return false;
}
var user=matchArray[1]
var domain=matchArray[2]
if (user.match(userPat)==null)
{
alert("The E-mail username doesn't seem to be valid.");
return false;
}
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null)
{
for (var i=1;i<=4;i++)
{
if (IPArray[i]>255)
{
alert("Destination IP address is invalid!")
col.focus();
return false
}
}
return true
}
var domainArray=domain.match(domainPat)
if (domainArray==null)
{
alert("The domain name doesn't seem to be valid.")
col.focus();
return false
}
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 )
{
alert("The address must end in a three-letter domain, or two letter country.")
col.focus();
return false
}
if (len<2)
{
var errStr="This address is missing a hostname!"
alert(errStr);
col.focus();
return false;
}
return true;
}
//..........................................................................................
//CHECK THE WHETHER FROMDATE IS LESSER THAN TODATE OR BOTH DATES CAN BE EQUAL
//..........................................................................................
function fncCompareDates(from, to)
{
var strDay,strMonth,strYear,fromdate,todate;
var intfromdate,inttodate;
fromdate = from;
todate = to;
strDay = fromdate.substr(0,2);
strMonth = fromdate.substr(3,2);
strYear = fromdate.substr(6,4);
fromdate = strYear+strMonth+strDay;
strDay = todate.substr(0,2);
strMonth = todate.substr(3,2);
strYear = todate.substr(6,4);
todate = strYear+strMonth+strDay;
intfromdate=parseInt(fromdate, 10);
inttodate=parseInt(todate, 10);
if (intfromdate <= inttodate)
{
return true;
}
else
{
if (from.value == "" || to.value == "")
{
alert("Both the dates must be entered");
return false;
}
else
{
return false;
}
}
}
//CHECK ALL THE CHECKBOX DEPEND ON THE STATE OF CHECKBOX SELECTION AT THE TOP
function fncCheckAllDGrid(aspCheckBoxID, checkVal)
{
re = new RegExp(':' + aspCheckBoxID + '$');
//generated controlname starts with a colon
for(i = 0; i < document.forms[0].elements.length; i++)
{
elm = document.forms[0].elements[i]
if ((elm.type == 'checkbox') && !(elm.disabled))
{
if (re.test(elm.name))
{
elm.checked = checkVal
}
}
}
}
function fncUnCheckHeader(PGridId,aspCheckBoxHeaderID,checkStatus)
{
strCBMid=PGridId+'__ctl1_'+aspCheckBoxHeaderID
if (checkStatus==false)
{
document.all(strCBMid).checked=false;
}
}
//....................................................................................
// CHECKING WHETHER TIME IS VALID OR NOT
//10:50...............................................................................
function fncIsTimeValid(strTime)
{
if (strTime.length != 5)
{
return false;
}
var iHour = parseInt(strTime.substring(0,2))
var iColon = strTime.substring(2,3)
var iMinute = parseInt(strTime.substring(3,5))
if (iColon != ":")
{
return false;
}
if ((iHour < 0) || (iHour > 23))
{
return false;
}
if ((iMinute < 0) || (iMinute > 59))
{
return false;
}
return true;
}
//CHECKING WHETHER CHAR IS APPEARED MORE THAN ONCE IN VALUE STRING
function IsCharExist(value,Char)
{
var status=new String(value);
var len = status.length;
var lCPeriodPosition = -1;
//CHECK CHAR IS IN THE STRING
if (status.indexOf(Char,0)>=0)
{
for(var i=0;i<len;i++)
{
if(status.charAt(i)==Char)
{
lCPeriodPosition++;
}
}
}
if (status.indexOf(Char,0)>=0)
{
if (lCPeriodPosition>0)
{
return false;
}
}
if (status.indexOf(Char)>=-1)
{
return true;
}
}
Related articles
Related discussion
-
How to pass value from a Pop up window to Content Page in asp.net C#pl
by Mulish Mehdi (5 replies)
-
javascript calender in datagrid
by ramdhavepreetam (1 replies)
-
How to call ASp.Net Vb Class function By JavaScript
by gitsiva (2 replies)
-
Debug javascript
by anilsjce (0 replies)
-
How to access a javascript variable from Csharp code?
by laltu4pdey (1 replies)
Events coming up
-
Oct
14
What’s New in Visual Studio 2008 Service Pack 1?
Birmingham, United Kingdom
“Service Pack? We’re calling it a Service Pack? Are you kidding??!?!” Visual Studio 2008 Service Pack 1 will release later in 2008 alongside .NET Framework V3.5 Service Pack 1 and, together, they represent a significant upgrade to Visual Studio 2008. There are enhancements across many areas of the .NET Framework such as data access, windows application development and web development and there are also corresponding changes in the development environment to support the new framework features.
Comments
Leave a comment
Sign in or Join us (it's free).