Simple JavaScript Functions

JavaScript

Double Dot Validation

ASP.NET Code:

  1. < asp: Label ID = "lblddId"  
  2. runat = "server"  
  3. Text = "DoubleDot" > < /asp:Label>  
  4.   
  5. <asp:TextBox ID="TextBox1" runat="server" onmouseout="return Validate();"></asp: TextBox >  
  6.   
  7. function Validate() {  
  8.   
  9.     /*Without using Regular Expression*/  
  10.   
  11.     var phn = document.getElementById('TextBox1').value;  
  12.   
  13.     var i = 0;  
  14.   
  15.     if (phn.length > 0) {  
  16.   
  17.         for (var j = 0; j < phn.length; j++) {  
  18.   
  19.             if (phn.charAt(j) == ".") {  
  20.   
  21.                 if (i > 0) {  
  22.   
  23.                     alert("more dots");  
  24.   
  25.                 } else {  
  26.   
  27.                     i++;  
  28.   
  29.                 }  
  30.   
  31.             }  
  32.   
  33.         }  
  34.   
  35.     } else {  
  36.   
  37.         alert("Enter a value..");  
  38.   
  39.     }  
  40.   
  41.     /*Using Regular Expression*/  
  42.     /*SIMPLE ONE*/  
  43.   
  44.     var phn = document.getElementById('TextBox1').value;  
  45.   
  46.     if (1 < phn.match(/\./g).length) {  
  47.   
  48.         alert("More Dots");  
  49.   
  50.     } else {  
  51.   
  52.         alert("Single Dot");  
  53.   
  54.     }  
  55.   
  56. }  
Result:

    Input: 254..24

    Output: More Dots

Double Dot Validation

RoundOff the Zero

ASP.NET Code:

  1. < asp: Label ID = "lblROffId"  
  2. runat = "server"  
  3. Text = "RoundOff" > < /asp:Label>  
  4.   
  5. <asp:TextBox ID="txtRoundId" runat="server"></asp: TextBox >  
  6.   
  7. < asp: Button ID = "btnRoundId"  
  8. runat = "server"  
  9. Text = "Click"  
  10. OnClientClick = "return RoundOff();" / >  
  11.   
  12. function RoundOff() {  
  13.   
  14.     /*Without using Regular Expression*/  
  15.   
  16.     var a = document.getElementById('txtRoundId').value;  
  17.   
  18.     if (a.length > 1) {  
  19.   
  20.         if (a.charAt(0) == "0") {  
  21.   
  22.             for (var b = 0; b < a.length; b++) {  
  23.   
  24.                 if (a.charAt(b).match(/\./g)) {  
  25.   
  26.                     if (1 < a.match(/\./g).length) {  
  27.   
  28.                         alert("More dots were found.");  
  29.   
  30.                         break;  
  31.   
  32.                     } else {  
  33.   
  34.                         var rslt = "0" + a.charAt(b) + a.slice(b + 1); //slice() - returns selected element from an array. syntax: arrayvar.slice(start,end)  
  35.   
  36.                         alert(rslt);  
  37.   
  38.                         break;  
  39.   
  40.                     }  
  41.   
  42.                 } else {  
  43.   
  44.                     if (a.charAt(b) >= 1) {  
  45.   
  46.                         var pos = a.indexOf(a.charAt(b));  
  47.   
  48.                         //indexOf() - returns the position of the character from the string.  
  49.   
  50.                         var rslt1 = a.slice(pos);  
  51.   
  52.                         alert(rslt1);  
  53.   
  54.                         break;  
  55.   
  56.                     }  
  57.   
  58.                 }  
  59.   
  60.             }  
  61.   
  62.         } else {  
  63.   
  64.             alert(a);  
  65.   
  66.         }  
  67.   
  68.     }  
  69.   
  70. }  
  71.   
  72. /*Using Regular Expression*/  
  73. /*SIMPLE ONE*/  
  74.   
  75. var a = document.getElementById('txtRoundId').value;  
  76.   
  77. 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:

  1. function UpperCaseTest() {  
  2.   
  3.     var uc = document.getElementById("txtUpperCaseId").value;  
  4.   
  5.     var reg = new RegExp(/[A-Z]/);  
  6.   
  7.     if (reg.test(uc.charAt(0))) { //Check the first of the string  
  8.   
  9.         alert("True");  
  10.   
  11.     } else {  
  12.   
  13.         alert("False");  
  14.   
  15.     }  
  16.   
  17. }  
Result:

    Input : Hi

    Output : True

UpperCase Test

Trim the String

ASP.NET Code:

  1. function TrimString() {  
  2.   
  3.    var trmStr = document.getElementById("txtTrmId").value;  
  4.   
  5.    document.getElementById("txtTrmId").value = trmStr.replace(/^\s+|\s+$/g,''); //+ => preceeding item will be matched one or more times  
  6.   
  7. }  
Result:

 

    Input: Hi C# //Note there is an space between the colon and the string

    Output: Hi C# //Here trims that spac
    e

Trim the String

Display Current Day&Date&Time

ASP.NET Code:

  1. (function() { //on page load  
  2.   
  3.     function GetCurDateTime() {  
  4.   
  5.         var nwHrs = "";  
  6.   
  7.         var today = new Date();  
  8.   
  9.         var dayList = ["Sunday""Monday""Tuesday""Wednesday""Thursday""Friday""Saturday"];  
  10.   
  11.         document.getElementById("txtCurDateId").value = dayList[today.getDay()];  
  12.   
  13.         var hrs = today.getHours() ? "PM" : "AM";  
  14.   
  15.         if (today.getHours() > 12) {  
  16.   
  17.             nwHrs = today.getHours() - 12;  
  18.   
  19.         } else {  
  20.   
  21.             nwHrs = today.getHours();  
  22.   
  23.         }  
  24.   
  25.         document.getElementById("txtCurTimeId").value = nwHrs + hrs + ":" + today.getMinutes() + ":" + today.getSeconds();  
  26.   
  27.     }  
  28.   
  29.     window.setInterval(GetCurDateTime, 1000);  
  30.   
  31. })();  
Result:

 

    Output: Thursday 7PM:28:28 //dispaly the current day and dynamically showing seconds.

Display Current Date&Time

Print Current Page

  1. function OnPrint() {  
  2.   
  3.    window.print();  
  4.   
  5. }  
Print Current Page

GetWebSite URL

ASP.NET Code:

  1. <asp:TextBox ID="txtWebURLId" runat="server" Text=""></asp:TextBox>  
  2. <asp:Button ID="btnGtWebURLId" runat="server" Text="GetURL" OnClientClick="return GetWebsiteURL();" />  
  3.   
  4. function GetWebsiteURL() {  
  5.   
  6.    document.getElementById("txtWebURLId").value = window.location.href;  
  7.   
  8. }  
GetWebSite URL

Highlight URL

ASP.NET Code:
  1. <asp:LinkButton ID="lnkId" runat="server" Text="Mouse Over On It" onmouseover="return HighLight();" onmouseout="return Normal();"></asp:LinkButton>  
  2.   
  3. <p id="paraId">Hi <b>C#</b></p>  
  4.   
  5. var boldvalues = document.getElementsByTagName("b");  
  6.   
  7. function HighLight() {  
  8.   
  9. for (var i = 0; i < boldvalues.length; i++) {  
  10.   
  11. boldvalues[i].style.color = 'red';  
  12.   
  13. }  
  14.   
  15. }  
  16.   
  17. function Normal() {  
  18.   
  19. for (var j = 0; j < boldvalues.length; j++) {  
  20.   
  21. boldvalues[j].style.color = 'black';  
  22.   
  23. }  
  24.   
  25. }  
Result:

 

    Input : Mouse over here Hi C#

    Output : Hi C#

Highlight URL

Blinking String

ASP.NET Code:

  1. <input id="blinkId" style="background-color:Red; color:White" value="Developers" />  
  2.   
  3. setInterval(BlinkFunctn, 500);  
  4.   
  5. var txt = "";  
  6.   
  7. var count = 0;  
  8.   
  9. function BlinkFunctn() {  
  10.   
  11. var a = document.getElementById("blinkId");  
  12.   
  13. if (count == 0) {  
  14.   
  15. txt = a.value;  
  16.   
  17. }  
  18.   
  19. if (count % 2 == 0) {  
  20.   
  21. a.value = "";  
  22.   
  23. }  
  24.   
  25. else {  
  26.   
  27. a.value = txt;  
  28.   
  29. }  
  30.   
  31. count++;  
  32.   
  33. }  
Result:

 

    Input : Developers

    Output : Developers

Blinking String

Validate String IsString Or Not

ASP.NET Code:

  1. < asp: Label ID = "Label1"  
  2. runat = "server"  
  3. Text = "Input String" > < /asp:Label>  
  4.   
  5. <asp:TextBox ID="txtStringId" runat="server"></asp: TextBox >  
  6.   
  7. < asp: Button ID = "Button1"  
  8. runat = "server"  
  9. Text = "Validate"  
  10. OnClientClick = "return ValStringOrNot()" / >  
  11.   
  12. function ValStringOrNot() {  
  13.   
  14.     var str = document.getElementById("txtStringId").value;  
  15.   
  16.     var val = new RegExp(/[^a-zA-Z]/g);  
  17.   
  18.     if (val.test(str)) {  
  19.   
  20.         alert("Please enter string");  
  21.   
  22.     } else {  
  23.   
  24.         alert("Hey,this is string");  
  25.   
  26.     }  
  27.   
  28.     /*FOR SWAPING UPPERCASE TO LOWER AND VICE VERSA*/  
  29.   
  30.     return str.replace(/([a-z]+)|([A-Z]+)/g, function(match, chr) {  
  31.   
  32.         return chr ? match.toUpperCase() : match.toLowerCase();  
  33.   
  34.     }  
Validate String IsString Or Not

Functions On Object
  1. function FunctionsOnObject() {  
  2.   
  3.     var employee = {  
  4.   
  5.         name: 'aaa',  
  6.   
  7.         designation: 'developer',  
  8.   
  9.         company: 'ccc'  
  10.   
  11.     }  
  12.   
  13.     /*Get the list of properties of an Object*/  
  14.   
  15.     var emp = [];  
  16.   
  17.     for (var i in employee) {  
  18.   
  19.         emp.push(i);  
  20.   
  21.     }  
  22.   
  23.     alert(emp.length); /*To get length of the object*/  
  24.   
  25.     alert(emp); /*O/P is name,designation,company*/  
  26.   
  27.     /*To delete a particular property*/  
  28.   
  29.     delete employee.company;  
  30.   
  31.     /*'delete' operator - removes property of an object*/  
  32.   
  33.     for (var j in employee) {  
  34.   
  35.         emp.push(j);  
  36.   
  37.     }  
  38.   
  39.     alert(emp);  
  40.   
  41. }  
Functions On Object
  1. SplitStrings To SubStrings  
  2.   
  3. function SplitStrToSubstring()   
  4. {  
  5.   
  6.     var abc = "you"/*Input*/  
  7.   
  8.     for (var c = 0; c < abc.length; c++)   
  9.     { /*Important*/  
  10.   
  11.         for (var d = c + 1; d < abc.length + 1; d++)   
  12.         {  
  13.   
  14.             alert(abc.slice(c, d)); /*Output - 'y','o','u','yo','ou','you'*/  
  15.   
  16.         }  
  17.   
  18.     }  
  19.   
  20. }  
SplitStrings To SubStrings

Factorial

Code
  1. function Factorial(f)   
  2. {  
  3.   
  4.     var no = 3; /*Input*/  
  5.   
  6.     var s;  
  7.   
  8.     for (var n = no; n > 0; n--)   
  9.     {  
  10.   
  11.         if (n == no)   
  12.         {  
  13.   
  14.             s = n * 1;  
  15.   
  16.         }   
  17.         else   
  18.         {  
  19.   
  20.             s = s * n;  
  21.   
  22.         }  
  23.   
  24.     }  
  25.   
  26.     alert(s); /*Output - 6*/  
  27.   
  28.     /*Simple method*/  
  29.   
  30.     if (f == 0)   
  31.     {  
  32.   
  33.         return 1;  
  34.   
  35.     }   
  36.     else   
  37.     {  
  38.   
  39.         return f * Factorial(f - 1);  
  40.   
  41.     }  
  42.   
  43.     * Simple method * /  
  44.   
  45. }  
  46.   
  47. alert(Factorial(5));