Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
XML
html code:
<script type ="text/javascript">
function AllowAlphabetName(e)
{
document.getElementById("div_name").innerHTML="";
    document.getElementById("div_name").style.display="none";

  isIE = document.all ? 1 : 0
  keyEntry = !isIE ? e.which : event.keyCode;
  if (((keyEntry >= '65') && (keyEntry <= '90')) || ((keyEntry >= '97') && (keyEntry <= '122')) || (keyEntry == '46') || (keyEntry == '32') || keyEntry == '45')
     return true;
  else
{
   document.getElementById("div_name").innerHTML="<B>Enter only Characters</B>";
            document.getElementById("div_name").style.display="block";

    return false;
      }
}
</script>
<asp:TextBox ID="textbox1" runat="server"></asp:TextBox>
<asp:div id="div_name"></asp:div>

c# code:
 protected void Page_Load(object sender, EventArgs e)
    {

        textbox1.Attributes.Add("onkeypress", "AllowAlphabetName(e)");
}

plz solution it,,,,,
Posted
Updated 21-Apr-13 0:47am
v7
Comments
Sergey Alexandrovich Kryukov 21-Apr-13 1:19am    
Which characters are you trying to filter out, exactly? What's the problem?
In fact, this is not a question.
—SA
Manfred Rudolf Bihy 21-Apr-13 5:39am    
Do you have a question to ask?
If your answer to that is yes, then please edit your question and add that question.

Any text box already allows to enter only some characters and nothing else, so the question does not make any sense at all.

The code does remotely resembles the code one would write in order to filter out some characters from the character input for an HTML input element, but no question was asked, no goals and no problems explained, so it all does not allow to provide more detailed answer.

Just few notes: if you mention something like "TextBox", always provide a fully qualified name of the type. The same simple class name can be used in different unrelated libraries. Not in all cases it's possible to see from the context that you use ASP.NET or something else. Also, don't forget to allow character #8, which is the backspace. By some reasons, this key is considered as a character and should not be filtered out.

—SA
 
Share this answer
 
Hi please try with event onKeyUp.
if you use event onKeyPress .this event fires before that pressed key recorded in text box
Hope to help
 
Share this answer
 
Comments
Member 9959371 21-Apr-13 1:38am    
Iam also try this properties(onKeyUp),but does not work
Ali Reza Barkhordari 21-Apr-13 1:46am    
you can do this easily with jQuery . if you want tell me to put the codes
Member 9959371 21-Apr-13 3:23am    
I want insert charcter in a textbox ,if i enter numeric that textbox,it will display message.my code
html code:
<script type ="text/javascript">
function AllowAlphabetName(e)
{
document.getElementById("div_name").innerHTML="";
document.getElementById("div_name").style.display="none";

isIE = document.all ? 1 : 0
keyEntry = !isIE ? e.which : event.keyCode;
if (((keyEntry >= '65') && (keyEntry <= '90')) || ((keyEntry >= '97') && (keyEntry <= '122')) || (keyEntry == '46') || (keyEntry == '32') || keyEntry == '45')
return true;
else
{
document.getElementById("div_name").innerHTML="Enter only Characters";
document.getElementById("div_name").style.display="block";

return false;
}
}
</script>
<asp:TextBox ID="txt_crpname" runat="server">
<asp:div id="div_name">

c# code:
protected void Page_Load(object sender, EventArgs e)
{

txt_crpname.Attributes.Add("onkeypress", "AllowAlphabetName(e)");
}
Ali Reza Barkhordari 21-Apr-13 9:16am    
This is easy, just try with this code ( NOTE :: use e.charCode NOT e.keyCode !!!)

IN HTML
<asp:TextBox ID="txt" />

IN SCRIPT
$('input#txt').keyPress(function(e){
if (some constraint on e.charCode not e.keyCode)
{

}
else //invalid
{
e.preventDefault
}
});

just this. sooo easy
Try this code
XML
<script type="text/javascript">
function ValidateChars(e)
{
isIE = document.all ? 1 : 0
keyEntry = !isIE ? e.which : event.keyCode;
if (((keyEntry >= '65') && (keyEntry <= '90')) || ((keyEntry >= '97') && (keyEntry <= '122')) || (keyEntry == '46') || (keyEntry == '32') || keyEntry == '45')
return true;
else
if (((keyEntry >= '47') && (keyEntry <= '56')) || ((keyEntry >= '97') && (keyEntry <= '122')) || (keyEntry == '46') || (keyEntry == '32') || keyEntry == '45')
{
alert('Number is entered');
return false;
}
else
return false;
}
</script>
<asp:TextBox ID="TextBox1" runat="server" onkeypress="return ValidateChars(event)" ></asp:TextBox>
 
Share this answer
 
v2

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