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

funcoes.js

 

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,"));

}

 

Default.aspx

 

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

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

  <title></title>

  <script src="funcoes.js" />

</head>

<body>

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

  <div>

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

  </div>

  </form>

</body>

</html>