ASP.NET - javascript to allow only numbers and special characters

Asked By Chinnarasu Chinnarasu.M on 29-Jul-11 01:44 AM
hi i need javascript to alow only numbers,specialcharacters,spaces...
any help???
Reena Jain replied to Chinnarasu Chinnarasu.M on 29-Jul-11 01:51 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 NotAllowAlphabet(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')
 {
alert('Please Enter Only Character values.');
   return false;
 
  else
 
{
 
  return true;
 
  }
 
}
<asp:TextBox ID="TextBox1" runat="server" MaxLength="1" onkeypress="return NotAllowAlphabet(event)"></asp:TextBox>
Ravi S replied to Chinnarasu Chinnarasu.M on 29-Jul-11 01:57 AM
HI

try this

onkeypress="javascript:allowNumeric();"

function allowNumeric() {
    var iKeyCode;

    iKeyCode = event.keyCode;

    if (iKeyCode < 48 || iKeyCode > 57) {
      event.keyCode = null;
    }
    return;
}
Riley K replied to Chinnarasu Chinnarasu.M on 29-Jul-11 02:00 AM
Use this Regex Exp '^[0-9a-zA-Z\. ]+$'

Try this code
 
function CheckName(txt)
  {
    var regName = new RegExp('^[0-9a-zA-Z\. ]+$');
    if (txt.value.match(regName) === null)
    {
    txt.value = txt.value.substr(0,(txt.value.length-1) );
    }
 
  }

<div>
    Name: <asp:TextBox ID="txtName" runat="server" onkeyup="CheckName(this);" onkeydown="CheckName(this);"></asp:TextBox>
     
  </div>

Jitendra Faye replied to Chinnarasu Chinnarasu.M on 29-Jul-11 02:02 AM
Use this javascript function-


function validate(keyCode)

{

if((keyCode >= 65 && keyCode <= 90) || (keyCode == 8 || keyCode == 32 )
//add more condition for character which you want to allow

 {
    retrun true;
  } 
else
{
 return false;
 }
}

Anoop S replied to Chinnarasu Chinnarasu.M on 29-Jul-11 03:20 AM
function fn_validateNumeric(thi,e)
{
    if (window.event) keycode = window.event.keyCode;
        else if (e) keycode = e.which;
        else return true;
   if (((keycode>=65) && (keycode<=90))||((keycode>=97) && (keycode<=122)))
   {
      return true;
   }
   else
   {
      return false;
   }
}

You can use regular expressions to do this

In your case the regex will be [a-zA-Z]+

Thanks



Anoop S replied to Chinnarasu Chinnarasu.M on 29-Jul-11 03:21 AM
sorry my code for not allow numbers and special characters