Allow Alphanumeric characters and Replace nonAlphanumeric with EmptyString


This code snippet is to allow only alphanumeric characters and replace the non alphanumeric characters of a textbox with empty string. If a NonAlphanumeric character is entered the javascript method should replace it with an empty string.

This code snippet is to allow only alphanumeric characters and replace the non alphanumeric characters of a textbox with empty string.


Assuming that you have a TextBox which should allow the user to enter only Alphabets and numerics. If a NonAlphanumeric character is entered the javascript method should replace it with an empty string.


<asp:TextBox ID="txtBackendKey" onkeyup="return IsalphaNumericValidate(this)" runat="server"></asp:TextBox>



Below is the Javascript method to allow only Alphanumeric characters.



//Accept only Alphanumeric value in Backend Key field.
function IsalphaNumericValidate(alphanumericChar) {
var txtBackend = document.getElementById("<%=txtBackendKey.ClientID %>");
var value = txtBackend.value;
var m_strOut = new String(value);
txtBackend.value = m_strOut.replace(/[^A-Za-z0-9]/g, '');
return m_sOut;

}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: