C# .NET - how to validate textbox to accept only characters

Asked By v k on 18-Aug-11 02:46 AM
hi...
I am using RAD Text box..I want to check that if user is entering the value in text box it should only character allow spacing its OK. I want it through JavaScript and should display the message box for user that should" enter character only" all this functionality should happen on click of Submit Button...
thanks!!!!!
Anoop S replied to v k on 18-Aug-11 02:49 AM
i am assuming you want to accept only alphabets. and not any special characters, if thats the case you can use the below sample. hope this helps

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:RegularExpressionValidator

ID="RegularExpressionValidator1"

runat="server"

ErrorMessage="only characters allowed"

ControlToValidate="TextBox1"

ValidationExpression="^[a-z]*$"></asp:RegularExpressionValidator>

 

dipa ahuja replied to v k on 18-Aug-11 02:49 AM
Untitled document
<script type="text/javascript" language="javascript">
   function chkValid2() {
 
     var Ename = document.getElementById("Name");
     var EnamePatrn = /^[A-Za-z]+$/;
     if (!EnamePatrn.test(Ename.value)) {
       alert("Enter Characters Only");
       return false;
     }
     else
       return true;
   }
</script>
     <asp:TextBox ID="Name" runat="server"></asp:TextBox>
<input type="button" id="click" runat="server" value="Click here" onclick="chkValid2();"/>
 
OR Using Validataor Control
 
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
 
<%-- RagularExpression Validator --%>
 
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ErrorMessage="Enter Characters Only" Display="Dynamic" ControlToValidate="TextBox1"
       ValidationExpression="^[a-zA-Z'.]{1,40}$">Enter Characters Only</asp:RegularExpressionValidator>
 

Venkat K replied to v k on 18-Aug-11 02:50 AM
Use javascript regular expressions:

function AcceptCharacters()
{
var regexLetter = /[a-zA-z]/;
if(!regexLetter.test(textbox.value)){
alert('Type only  character');
return false;
}

Thanks
Reena Jain replied to v k on 18-Aug-11 03:10 AM
Hi,

here is the code for you

private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= 65 && e.KeyChar <= 90) || (e.KeyChar >= 97 && e.KeyChar <= 122) || e.KeyChar == 32 || e.KeyChar == 8 || (e.KeyChar > 47 && e.KeyChar < 58) || (e.KeyChar == 46) || (e.KeyChar == 8))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}

I would like to give you keycode also for my above code
  • keychar 65 to 90 is for 'A' to 'Z'
  • keychar 97 to 122 is for 'a' to 'z'
  • keychar 8 is for backspace
  • keychar 47 to 58 is for 0 to 9
but i will suggest if you are using asp.net then you should use client side validation for it, like this
function AllowAlphabet(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
 
{
 
  alert('Please Enter Only Character values.');
 
  return false;
 
  }
 
}
<asp:TextBox ID="TextBox1" runat="server" MaxLength="1" onkeypress="return AllowAlphabet(event)"></asp:TextBox>

or
use customer validator, custom validator is just provide u put your own function to validate so u need to write code for validate character only using regular expression in case of using custom validator control
so better is u can use regular expression in Regularexpression validator control this is specific validator control for validate with regular expression
<asp:RegularExpressionValidator id="valRegEx" runat="server"
  ControlToValidate="textbox1"
  ValidationExpression="[^a-z][A-Z]+$"
  ErrorMessage="* entry only character value."
  display="dynamic">*
</asp:RegularExpressionValidator>

hope this will help you
v k replied to Reena Jain on 18-Aug-11 03:27 AM
hey thanks
 but i want to include space also.. how i can i do this. i.e. space can allowed
Reena Jain replied to v k on 18-Aug-11 03:34 AM
Hi,

here keycode = 32 is for space only, so you can use this javascript function for space also

function AllowAlphabet(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
 
{
 
  alert('Please Enter Only Character values.');
 
  return false;
 
  }
 
}
Devil Scorpio replied to v k on 18-Aug-11 10:41 AM
Hi,

You can validate the textbox to accept character only by using javascript

Here is two solution to do so

Solution1:-
<script>
function onkeydown(){
   var ch = String.fromCharCode(event.keyCode);
   var filter = /[a-zA-Z]/   ;
   if(!filter.test(ch)){
      event.returnValue = false;
   }
}
</script>


This can restrict the user to input only characters, but you still have to consider that user can paste numbers, or drag in into textbox.


Solution2:-
Use event.keyCode to accept only char values.
write a function in javascript and call it on onkeypress event of Textbox

function checkNum()
{
 
if ((event.keyCode > 64 && event.keyCode < 91) || (event.keyCode > 96 && event.keyCode < 123) || event.keyCode == 8)
   return true;
else
   {
     alert("Please enter only char");
     return false;
   }
 
}