Home > ASP.Net, Javascript > TextBox Validation with Javascript

TextBox Validation with Javascript


Hi

//——— Allow TextBox to accept number only ——-

try this example to validate Textbox input with javascript

<script type = "text/javascript">

function isNumeric(keyCode)
{
  return ((keyCode >= 48 && keyCode <= 57) || keyCode == 8 || (keyCode >= 96 && keyCode <= 105))
}

</script>

<asp:TextBox ID="txtNumeric" runat="server" onkeydown = "return isNumeric(event.keyCode);"
onpaste = "return false;"></asp:TextBox>

The above script will ensure that only Numbers (0-9) are entered into the textbox.

in code behind:

txtNumeric.Attributes.Add("onkeydown", "return isNumeric(event.keyCode);");
txtNumeric.Attributes.Add("onpaste", "return false");   

 

 

//———– TextBox with Only Alphabets allowed. ———

<script type = "text/javascript">

function isAlpha(keyCode)
{
   return ((keyCode >= 65 && keyCode <= 90) || keyCode == 8 )
}

</script>

The above script will ensure that only Alphabets (A – Z, a – z) are entered into the textbox.

 
//——– TextBox with Only Alphanumeric Characters allowed. ——-

<script type = "text/javascript">

function isAlphaNumeric(keyCode)
{
return(keyCode >= 48&&keyCode<=57)||(keyCode>=65&&keyCode<=90) || keyCode == 8 || (keyCode >= 96 && keyCode <= 105))
}

</script>

The above script will ensure that only Alphabets (A – Z, a – z) or Numbers (0 – 9) are entered into the textbox

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net, Javascript
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: