Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all

I want to validate a textbox using jquery.

Means :I don't want to allow the user to type '<' and '>'
If the end user try to type these two key then it will not type anything.

I want a javascript function which can be called by all the textbox present in the form

please help to implement this.

I searched in Google too much but didn't find anything.

Thanks in advance
Posted

<input type="text" id="txtTest" onkeypress="ValidateTextBox();" onblur="ValidateTextBox();"/>
 
Share this answer
 
hi,
use this javascript function on KeyDown event

JavaScript
function ValidateTextBox() {
              var txt = document.getElementById('txtTest').value;
              if (txt.indexOf('>') > 0 || txt.indexOf('<') > 0) {
                  alert('Invalid data.');
                  document.getElementById('txtTest').value = '';
              }
          }

ASP.NET
<asp:textbox id="txtTest" runat="server" onkeydown="ValidateTextBox();" xmlns:asp="#unknown"></asp:textbox>


you can also use HTML element
JavaScript
<input type="text" id="txtTest"  önkeydown="ValidateTextBox();" />
 
Share this answer
 
v3
Comments
Arunprasath Natarajan 21-Jun-12 8:06am    
f5
Vikram Bhatt 27-Jun-12 13:33pm    
This may not work in case of Copy + Paste. Please check while applying.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900