Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript HTML
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 20-Apr-13 20:00pm
Edited 21-Apr-13 1:47am
v7
Comments
Sergey Alexandrovich Kryukov at 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 R. Bihy at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
Member 9959371 at 21-Apr-13 1:38am
   
Iam also try this properties(onKeyUp),but does not work
abforce at 21-Apr-13 1:46am
   
you can do this easily with jQuery . if you want tell me to put the codes
Member 9959371 at 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)");
}
abforce at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try this code
<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>
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 7,853
1 Sergey Alexandrovich Kryukov 7,107
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 21 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100