Share Blog

Saturday, June 30, 2018

Allow only numeric (0-9) Or Numeric With Decimal in HTML inputbox using jQuery


How to allow only numeric (0-9) Or Numeric With Decimal in HTML inputbox using jQuery?


I am creating a web page where I have an input text field in which I want to allow only numeric characters like (0,1,2,3,4,5...9) 0-9.


<script>
$(document).ready(function () {
//Allow Number Only in Textbox
$(".allownumber").on("keypress keyup blur", function (event) {
$(this).val($(this).val().replace(/[^\d].+/, ""));
if ((event.which < 48 || event.which > 57)) {
event.preventDefault();
}
});
//Allow Number Only in Textbox

//Allow Number With decimal Only in Textbox
$(".allownumberwithdecimal").on("keypress keyup blur", function (event) {
//this.value = this.value.replace(/[^0-9\.]/g,'');
$(this).val($(this).val().replace(/[^0-9\.]/g, ''));
if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
event.preventDefault();
}
});
//Allow Number With decimal Only in Textbox
});
</script>



<span>Float</span>
<input type="text" name="numeric" class='allownumberwithdecimal'>
<div>Numeric values only allowed (With Decimal Point) </div>
<br/> <br/> <br/>
<span>Int</span>
<input type="text" name="numeric" class='allownumber'>
<div>Numeric values only allowed (Without Decimal Point) </div>



Disable Browser Back Button after LogOut in ASP.Net using JavaScript


How to disable Back button in Browser after Logout using JavaScript.

Browser Back button cannot be disabled and hence in order to prevent User navigating to previous page, the User is redirected back to the Current page forcefully using JavaScript.

Disable Browser Back Button Script
The following JavaScript code snippet must be placed in the HEAD section of the Page where the User must be prevented from going back.
<script type = "text/javascript" >
   function preventBack(){window.history.forward();}
    setTimeout("preventBack()", 0);
    window.onunload=function(){null};
</script>

Home Page

The HTML Markup of Home page consists of an HTML Anchor link to the Logout page.
The Disable Browser Back Button Script is placed in the HEAD section so that User cannot access the Home page using Browser Back button from Logout page.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Home</title>
<script type="text/javascript">
function preventBack() { window.history.forward(); }
setTimeout("preventBack()", 0);
window.onunload = function () { null };
</script>
</head>
<body>
<h3>Home</h3>
<hr />
<a href="Logout.aspx">Logout</a>
</body>
</html>


Logout Page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="logout.aspx.cs" Inherits="Test" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Logout</title>
</head>
<body>
<h3>Logout</h3>
</body>
</html>



Wednesday, June 20, 2018

Remove Last Character from String in C#


Here I will explain how to remove last character from string in C#,with example or delete or remove last character in string with examples in C#.We can easily remove last character from string by using Remove or Trim or IndexOf properties.

Method 1

Following is the one way of removing last character from string using Remove property

string inputstring = "1,2,3,4,5,6,7,8,9,10,11,";

string outputstring = inputstring.Remove(inputstring.Length - 1, 1);

Console.Write(outputstring);

Console.ReadLine();


Method 2

Following is the another way of removing last character from string using Trim property


string inputstring = "1,2,3,4,5,6,7,8,9,10,11,";

string outputstring = inputstring.Remove(inputstring.LastIndexOf(","));

Console.Write(outputstring);

Console.ReadLine();


Method 3

Following is the another way of removing the last character from string using IndexOf property but that character should exists only one time otherwise this property will return the values before the first character.


string inputstring = "1,2,3,4,5,6,7,8,9,10,11,";

string outputstring = inputstring.Trim(",".ToCharArray());

Console.Write(outputstring);

Console.ReadLine();


How to extract data from string in C#

I getting data in my application through Code. Code returns data in several parameters/variables. One of the parameter return below string.



string input = @"Some text BLA/123/5345/349230498 some more text PNR: 12345678, Name: John, CallName: Peter, TimeStamp: 01.10.2015";
string value = String.Empty;

List<string> keyValuePairs = input.Split(',').ToList();

foreach (var keyValuePair in keyValuePairs)
{
string key = keyValuePair.Split(':')[0].Trim();
if (key == "Name")
{
value = keyValuePair.Split(':')[1];
}
}

Disable Cut, Copy and Paste in TextBox using AngularJS in ASP.Net

How to Disable Cut, Copy and Paste in TextBox or TextArea using AngularJS in ASP.Net.

Cut, Copy and Paste operations in TextBox or TextArea can be performed using CTRL button or using Mouse Right Click.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestJavaScript.aspx.cs" Inherits="TestJavaScript" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Disable Cut, Copy and Paste in TextBox using AngularJS</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', [])
app.controller('MyController', function ($scope) {
$scope.AttachEvent = function (control, eventName) {
if (control.addEventListener) {
control.addEventListener(eventName, function (e) { e.preventDefault(); }, false);
} else if (control.attachEvent) {
control.attachEvent('on' + eventName, function () { return false; });
}
};
var controls = document.getElementsByTagName("*");
var regEx = new RegExp("(^| )disable( |$)");
for (var i = 0; i < controls.length; i++) {
if (regEx.test(controls[i].className)) {
$scope.AttachEvent(controls[i], "copy");
$scope.AttachEvent(controls[i], "paste");
$scope.AttachEvent(controls[i], "cut");
}
}
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div ng-app="MyApp" ng-controller="MyController">
Enter Text <asp:TextBox ID="txtbox1" runat="server" CssClass="disable"></asp:TextBox><br />
<br />
Paste Text <asp:TextBox ID="txtbox2" runat="server" CssClass="disable" TextMode="MultiLine" Rows="4" Columns="20"></asp:TextBox>
</div>
</form>
</body>
</html>