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() {
-
-
-
- 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..");
-
- }
-
-
-
-
- var phn = document.getElementById('TextBox1').value;
-
- if (1 < phn.match(/\./g).length) {
-
- alert("More Dots");
-
- } else {
-
- alert("Single Dot");
-
- }
-
- }
Result: 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() {
-
-
-
- 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);
-
- alert(rslt);
-
- break;
-
- }
-
- } else {
-
- if (a.charAt(b) >= 1) {
-
- var pos = a.indexOf(a.charAt(b));
-
-
-
- var rslt1 = a.slice(pos);
-
- alert(rslt1);
-
- break;
-
- }
-
- }
-
- }
-
- } else {
-
- alert(a);
-
- }
-
- }
-
- }
-
-
-
-
- var a = document.getElementById('txtRoundId').value;
-
- alert(a.replace(/^0+(?!\.|$)/, ''));
Result: 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))) {
-
- alert("True");
-
- } else {
-
- alert("False");
-
- }
-
- }
Result: 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,'');
-
- }
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() {
-
- 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:
Display Current Date&Time
Print Current Page
- function OnPrint() {
-
- window.print();
-
- }
Print Current Page
GetWebSite 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 URL
Highlight 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:
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:
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");
-
- }
-
-
-
- return str.replace(/([a-z]+)|([A-Z]+)/g, function(match, chr) {
-
- return chr ? match.toUpperCase() : match.toLowerCase();
-
- }
Validate String IsString Or Not
Functions On Object
- function FunctionsOnObject() {
-
- var employee = {
-
- name: 'aaa',
-
- designation: 'developer',
-
- company: 'ccc'
-
- }
-
-
-
- var emp = [];
-
- for (var i in employee) {
-
- emp.push(i);
-
- }
-
- alert(emp.length);
-
- alert(emp);
-
-
-
- delete employee.company;
-
-
-
- for (var j in employee) {
-
- emp.push(j);
-
- }
-
- alert(emp);
-
- }
Functions On Object
- SplitStrings To SubStrings
-
- function SplitStrToSubstring()
- {
-
- var abc = "you";
-
- for (var c = 0; c < abc.length; c++)
- {
-
- for (var d = c + 1; d < abc.length + 1; d++)
- {
-
- alert(abc.slice(c, d));
-
- }
-
- }
-
- }
SplitStrings To SubStrings
Factorial
Code
- function Factorial(f)
- {
-
- var no = 3;
-
- var s;
-
- for (var n = no; n > 0; n--)
- {
-
- if (n == no)
- {
-
- s = n * 1;
-
- }
- else
- {
-
- s = s * n;
-
- }
-
- }
-
- alert(s);
-
-
-
- if (f == 0)
- {
-
- return 1;
-
- }
- else
- {
-
- return f * Factorial(f - 1);
-
- }
-
- * Simple method * /
-
- }
-
- alert(Factorial(5));