Microsoft Dot Net Master

Microsoft Dot Net Master
Microsoft Dot Net Master

Monday, April 22, 2013

jQuery Allow Only Numbers in Textbox using JavaScript


To restrict user to enter only numbers in textbox we need to write the following code


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery allow only numbers or numeric characters in textbox</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function () {
$('#txtNumeric').keydown(function (e) {
if (e.shiftKey || e.ctrlKey || e.altKey) {
e.preventDefault();
else {
var key = e.keyCode;
if (!((key == 8) || (key == 46) || (key >= 35 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105))) {
e.preventDefault();
}
}
});
});
</script>
</head>
<body>
<div>
<input type="text" id="txtNumeric" />
</div>
</body>
</html>

jQuery Allow Only Alphabets in Textbox using JavaScript


To allow only alphabets in textbox using JQuery we need to write the following code


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Allow only alphabets in textbox using jquery</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function () {
$('#txtNumeric').keydown(function (e) {
if (e.shiftKey || e.ctrlKey || e.altKey) {
e.preventDefault();
else {
var key = e.keyCode;
if (!((key == 8) || (key == 32) || (key == 46) || (key >= 35 && key <= 40) || (key >= 65 && key <= 90))) {
e.preventDefault();
}
}
});
});
</script>
</head>
<body>
<div>
<b>Enter Text:</b><input type="text" id="txtNumeric" />
</div>
</body>
</html>

jQuery Allow Alphanumeric (Alphabets & Numbers) Characters in Textbox using JavaScript


To allow only alphabets & numbers in textbox using JQuery we need to write the following code


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Allow only alphanumeric characters in textbox</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function () {
$('#txtNumeric').keydown(function (e) {
if (e.shiftKey || e.ctrlKey || e.altKey) {
e.preventDefault();
else {
var key = e.keyCode;
if (!((key == 8) || (key == 32) || (key == 46) || (key >= 35 && key <= 40) || (key >= 65 && key <= 90) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105))) {
e.preventDefault();
}
}
});
});
</script>
</head>
<body>
<div>
<b>Enter Text:</b><input type="text" id="txtNumeric" />
</div>
</body>
</html>

jQuery Disable Cut Copy Paste Options in Textbox


To disable cut, copy and paste functionality for the Textbox we need to write the code like as shown below


<script type="text/javascript">
$(function() {
$('#txtName').bind("cut copy paste"function(e) {
e.preventDefault();
});
});
</script>
If you want to see it in complete example write the code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Disable Cut, Copy and Paste Options in textbox using jQuery</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function() {
$('#txtName').bind("cut copy paste"function(e) {
e.preventDefault();
});
});
</script>
</head>
<body>
<div>
<input type="text" id="txtName" />
</div>
</body>
</html>

Friday, April 19, 2013

How to Insert Values into Identity Column


To insert value in identity column I will explain with one example for that first create one sample table like as shown below


CREATE TABLE UserDtls
 (
 UserId int PRIMARY KEY IDENTITY,
 UserName varchar(120),
 Qualification varchar(50)
 )
Once we create UserDtls insert data like as shown below


INSERT INTO UserDtls(UserId,UserName,Qualification) VALUES(1,'Suresh','B.Tech')
Whenever we run above query we will get error message like as shown below


Msg 544, Level 16, State 1, Line 1
Cannot insert explicit value for identity column in table 'UserDtls' when IDENTITY_INSERT is set to OFF.
Based on above error message we can realize that identity columns won’t allow to insert new values whenIDENTITY_INSERT is OFF .To solve this problem we need to set is ON for that we need to write the code like as shown below


SET IDENTITY_INSERT UserDtls ON
INSERT INTO UserDtls(UserId,UserName,Qualification) VALUES(1,'Suresh','B.Tech')
INSERT INTO UserDtls(UserId,UserName,Qualification) VALUES(2,'Rohini','MSC')
SET IDENTITY_INSERT UserDtls OFF
Once we run above query our Output will be like this

---------------------------------------
(1 row(s) affected)

(1 row(s) affected)
In this way we can insert values to identity columns in SQL Server.