JavaScript
Double Dot Validation
ASP.NET Code:
< asp: Label ID = "lblddId"
runat = "server"
Text = "DoubleDot" > < /asp:Label>
<asp:TextBox ID="TextBox1" runat="server" onmouseout="return Validate();"></asp: TextBox >
function Validate() {
/*Without using Regular Expression*/
var phn = document.getElementById('TextBox1').value;
var i = 0;
if (phn.length > 0) {
for (var j = 0; j < phn.length; j++) {
if (phn.charAt(j) == ".") {
if (i > 0) {
alert("more dots");
} else {
i++;
}
}
}
} else {
alert("Enter a value..");
}
/*Using Regular Expression*/
/*SIMPLE ONE*/
var phn = document.getElementById('TextBox1').value;
if (1 < phn.match(/\./g).length) {
alert("More Dots");
} else {
alert("Single Dot");
}
}Result:Input: 254..24
Output: More Dots
Double Dot Validation
RoundOff the Zero
ASP.NET Code:
< asp: Label ID = "lblROffId"
runat = "server"
Text = "RoundOff" > < /asp:Label>
<asp:TextBox ID="txtRoundId" runat="server"></asp: TextBox >
< asp: Button ID = "btnRoundId"
runat = "server"
Text = "Click"
OnClientClick = "return RoundOff();" / >
function RoundOff() {
/*Without using Regular Expression*/
var a = document.getElementById('txtRoundId').value;
if (a.length > 1) {
if (a.charAt(0) == "0") {
for (var b = 0; b < a.length; b++) {
if (a.charAt(b).match(/\./g)) {
if (1 < a.match(/\./g).length) {
alert("More dots were found.");
break;
} else {
var rslt = "0" + a.charAt(b) + a.slice(b + 1); //slice() - returns selected element from an array. syntax: arrayvar.slice(start,end)
alert(rslt);
break;
}
} else {
if (a.charAt(b) >= 1) {
var pos = a.indexOf(a.charAt(b));
//indexOf() - returns the position of the character from the string.
var rslt1 = a.slice(pos);
alert(rslt1);
break;
}
}
}
} else {
alert(a);
}
}
}
/*Using Regular Expression*/
/*SIMPLE ONE*/
var a = document.getElementById('txtRoundId').value;
alert(a.replace(/^0+(?!\.|$)/, ''));Result:Input : 000.564654
Output : 0.564654
Input : 0.75000
Output : 0.75000
RoundOff the Zero
UpperCase Tes.
ASP.NET Code:
function UpperCaseTest() {
var uc = document.getElementById("txtUpperCaseId").value;
var reg = new RegExp(/[A-Z]/);
if (reg.test(uc.charAt(0))) { //Check the first of the string
alert("True");
} else {
alert("False");
}
}Result:Input : Hi
Output : True
UpperCase Test
Trim the String
ASP.NET Code:
function TrimString() {
var trmStr = document.getElementById("txtTrmId").value;
document.getElementById("txtTrmId").value = trmStr.replace(/^\s+|\s+$/g,''); //+ => preceeding item will be matched one or more times
}Result:
Input: Hi C# //Note there is an space between the colon and the string
Output: Hi C# //Here trims that space
Trim the String
Display Current Day&Date&Time
ASP.NET Code:
(function() { //on page load
function GetCurDateTime() {
var nwHrs = "";
var today = new Date();
var dayList = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
document.getElementById("txtCurDateId").value = dayList[today.getDay()];
var hrs = today.getHours() ? "PM" : "AM";
if (today.getHours() > 12) {
nwHrs = today.getHours() - 12;
} else {
nwHrs = today.getHours();
}
document.getElementById("txtCurTimeId").value = nwHrs + hrs + ":" + today.getMinutes() + ":" + today.getSeconds();
}
window.setInterval(GetCurDateTime, 1000);
})();Result:
Output: Thursday 7PM:28:28 //dispaly the current day and dynamically showing seconds.
Display Current Date&Time
Print Current Page
function OnPrint() {
window.print();
}Print Current PageGetWebSite URL
ASP.NET Code:
<asp:TextBox ID="txtWebURLId" runat="server" Text=""></asp:TextBox>
<asp:Button ID="btnGtWebURLId" runat="server" Text="GetURL" OnClientClick="return GetWebsiteURL();" />
function GetWebsiteURL() {
document.getElementById("txtWebURLId").value = window.location.href;
}GetWebSite URLHighlight URL
ASP.NET Code:
<asp:LinkButton ID="lnkId" runat="server" Text="Mouse Over On It" onmouseover="return HighLight();" onmouseout="return Normal();"></asp:LinkButton>
<p id="paraId">Hi <b>C#</b></p>
var boldvalues = document.getElementsByTagName("b");
function HighLight() {
for (var i = 0; i < boldvalues.length; i++) {
boldvalues[i].style.color = 'red';
}
}
function Normal() {
for (var j = 0; j < boldvalues.length; j++) {
boldvalues[j].style.color = 'black';
}
}Result:
Input : Mouse over here Hi C#
Output : Hi C#
Highlight URL
Blinking String
ASP.NET Code:
<input id="blinkId" style="background-color:Red; color:White" value="Developers" />
setInterval(BlinkFunctn, 500);
var txt = "";
var count = 0;
function BlinkFunctn() {
var a = document.getElementById("blinkId");
if (count == 0) {
txt = a.value;
}
if (count % 2 == 0) {
a.value = "";
}
else {
a.value = txt;
}
count++;
}Result:
Input : Developers
Output : Developers
Blinking String
Validate String IsString Or Not
ASP.NET Code:
< asp: Label ID = "Label1"
runat = "server"
Text = "Input String" > < /asp:Label>
<asp:TextBox ID="txtStringId" runat="server"></asp: TextBox >
< asp: Button ID = "Button1"
runat = "server"
Text = "Validate"
OnClientClick = "return ValStringOrNot()" / >
function ValStringOrNot() {
var str = document.getElementById("txtStringId").value;
var val = new RegExp(/[^a-zA-Z]/g);
if (val.test(str)) {
alert("Please enter string");
} else {
alert("Hey,this is string");
}
/*FOR SWAPING UPPERCASE TO LOWER AND VICE VERSA*/
return str.replace(/([a-z]+)|([A-Z]+)/g, function(match, chr) {
return chr ? match.toUpperCase() : match.toLowerCase();
}Validate String IsString Or NotFunctions On Object
function FunctionsOnObject() {
var employee = {
name: 'aaa',
designation: 'developer',
company: 'ccc'
}
/*Get the list of properties of an Object*/
var emp = [];
for (var i in employee) {
emp.push(i);
}
alert(emp.length); /*To get length of the object*/
alert(emp); /*O/P is name,designation,company*/
/*To delete a particular property*/
delete employee.company;
/*'delete' operator - removes property of an object*/
for (var j in employee) {
emp.push(j);
}
alert(emp);
}Functions On ObjectSplitStrings To SubStrings
function SplitStrToSubstring()
{
var abc = "you"; /*Input*/
for (var c = 0; c < abc.length; c++)
{ /*Important*/
for (var d = c + 1; d < abc.length + 1; d++)
{
alert(abc.slice(c, d)); /*Output - 'y','o','u','yo','ou','you'*/
}
}
}SplitStrings To SubStringsFactorial
Code
function Factorial(f)
{
var no = 3; /*Input*/
var s;
for (var n = no; n > 0; n--)
{
if (n == no)
{
s = n * 1;
}
else
{
s = s * n;
}
}
alert(s); /*Output - 6*/
/*Simple method*/
if (f == 0)
{
return 1;
}
else
{
return f * Factorial(f - 1);
}
* Simple method * /
}
alert(Factorial(5));