.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Javascript >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Code to restrict <> characters in textbox using JavaScript

Posted By:Virendra Dugar       Posted Date: November 29, 2009    Points: 10    Category: Javascript    URL: http://www.dotnetspark.com  
 

Code to restrict <> characters in textbox using JavaScript.

If you donot want user to enter <, > characters in text box, as this creats a security threat then we can disable it usnig JavaScript. 

Call this JavaScript function on Page_Load using RegisterStartupScript method. 

function RestrictCharacter()

{
var element;
var len=document.forms[0].elements.length;

for(i=0;i {
element=document.forms[0].elements;

if(element.type=='text' || element.type=='textarea')
{
element.attachEvent("onkeypress",function(){if(event.keyCode == 60 || event.keyCode==62 ||
event.keyCode==38 || event.keyCode==39)return false;});
element.attachEvent("onpaste",function()
{
var REGX=new RegExp('<|>|&|\'','gi');
var val=window.clipboardData.getData('Text');
if(REGX.test(val))
return false;
});
element.attachEvent("ondrop",function(){return false;});
}
}
}


This function will not only restrict user to enter these character (<,>), it will also not allow user to paste and drag and drop these character. 

Code to call this function in Page_Load 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Page.RegisterStartupScript(Me, Me.GetType(), "RestrictCharacters", "RestrictCharacter();", True)
End Sub


Hope this helps!!!!!

     

Further Readings:

Responses
Author: Dustin Massingale         Company URL: http://www.dotnetspark.com
Posted Date: September 28, 2010

Tested not working in IE6, IE7, IE8 and Firefox 3.6, IE6. Could enter < and >. Used ASP.NET and then HTML.

HTML version Below:

<code>
<html>

<head>
<script type="text/javascript">

function RestrictCharacter()
{
var element;
var len=document.forms[0].elements.length;

for(i=0;i<len;i++)
{
element=document.forms[0].elements;

if(element.type=='text' || element.type=='textarea')
{
element.attachEvent("onkeypress",function(){if(event.keyCode == 60 || event.keyCode==62 ||
event.keyCode==38 || event.keyCode==39)return false;});
element.attachEvent("onpaste",function()
{
var REGX=new RegExp('<|>|&|\'','gi');
var val=window.clipboardData.getData('Text');
if(REGX.test(val))
return false;
});
element.attachEvent("ondrop",function(){return false;});
}
}
}

</script>
</head>
<body onload="RestrictCharacter()">
<h1> Sample of Restricting Values from Textboxes </h1>
<br/>
Textbox
<input type="text"></input>
</body>
</html>
</code>

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend