Category >
ASP.NET
|| Published on :
Wednesday, February 10, 2016 || Views:
10087
||
How to prevent input field from special character prevent input field from special character
Introduction
Here Pawan Kumar will explain how to prevent input field from special character.
Description
In previous post I have explained
jQuery to Validate File Type and Size before Uploading through Asp.Net FileUpload Control,
How to clear the file upload control value using jQuery / JavaScript,
Export GridView selected rows to Excel or word in ASP.NET using CSharp,
How to allow numbers, backspace, delete, left and right arrow and Tab Keys to the TextBox using Javascript or JQuery in ASP.NET,
Upload And Read Excel File into DataSet in Asp.Net using C#,
How to find third highest salary in sql server., and many more articles.
Now I will explain How to How to prevent input field from special character
So follow the steps to learn How to prevent input field from special character
Step 1: Create a new website using Visual Studio 2010.
Step 2: Add a new webform and name it default.aspx.
Step 3: Add the following code to the aspx webpage
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript">
function check(e) {
var keynum
var keychar
var numcheck
// For Internet Explorer
if (window.event) {
keynum = e.keyCode;
}
// For Netscape/Firefox/Opera
else if (e.which) {
keynum = e.which;
}
keychar = String.fromCharCode(keynum);
//List of special characters you want to restrict
if (keychar == "'" || keychar == "`" || keychar == "!" || keychar == "@" || keychar == "#" || keychar == "$" || keychar == "%" || keychar == "^" || keychar == "&" || keychar == "*" || keychar == "(" || keychar == ")" || keychar == "-" || keychar == "_" || keychar == "+" || keychar == "=" || keychar == "/" || keychar == "~" || keychar == "<" || keychar == ">" || keychar == "," || keychar == ";" || keychar == ":" || keychar == "|" || keychar == "?" || keychar == "{" || keychar == "}" || keychar == "[" || keychar == "]" || keychar == "¬" || keychar == "£" || keychar == '"' || keychar == "\\") {
return false;
} else {
return true;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
User id :
<input type="text" id="txtname" name="txtname" onkeypress="return check(event)" />
</div>
</form>
</body>
</html>
Output:-
Conclusion:
So, In this tutorial we have learned, How to prevent input field from special character
Download Source Codes