Função para permitir somente valor num TextBox

<< Click to Display Table of Contents >>

Navigation:  Miscelânea >

Função para permitir somente valor num TextBox

Previous pageReturn to chapter overviewNext page



function isNum(caracter, caracteres) {

  if (caracteres.indexOf(caracter) == -1)

      return false;

  return true;



function SomenteValores(campo, event) {

  var BACKSPACE = 8;

  var key;

   CheckTAB = true;


   key = String.fromCharCode(event.keyCode);

  if (event.keyCode == BACKSPACE)

      return true;


  if (key == ',')

      if (campo.value.indexOf(',') != -1)

          return false;


  return (isNum(key, "0123456789,"));





<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HelloJunior.Default" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">

<head runat="server">


  <script src="funcoes.js" />



  <form id="form1" runat="server">


      <asp:TextBox ID="txtValor" runat="server" Width="75px" onKeyPress="return SomenteValores(this, event)"></asp:TextBox>