Jump to content

text field


ricardocoimbra
 Share

Recommended Posts

ricardocoimbra

Bom eu pretendo que num text field ele só aceite numeros e meto assim:

<SCRIPT>
function teclas(formato){
         switch (formato){
                 case 'numerico':
                       if (window.event.keyCode < 48 || window.event.keyCode > 57){
                           event.returnValue = false;
                       }
                       break;
                 case 'espaco':
                       if (window.event.keyCode == 32){
                           event.returnValue = false;
                       }
                       break;
                 case 'alfabeto':
                       if (window.event.keyCode < 48 || (window.event.keyCode > 57 && window.event.keyCode < 97) || window.event.keyCode > 122){
                           event.returnValue = false;
                       }
                 case 'seguro':
                       if ((window.event.keyCode < 48 && (window.event.keyCode != 13 && window.event.keyCode != 32)) || (window.event.keyCode > 57 && window.event.keyCode < 97 && window.event.keyCode != 95) || window.event.keyCode > 122){
                            event.returnValue = false;
                       } else if (window.event.keyCode == 32){
                            window.event.keyCode = 95;
                       }
                       break;
                 case 'email':
                       if ((window.event.keyCode < 48 && (window.event.keyCode != 13 && window.event.keyCode != 45 && window.event.keyCode != 32 && window.event.keyCode != 46)) || (window.event.keyCode > 57 && window.event.keyCode < 97 && window.event.keyCode != 95 && window.event.keyCode != 64) || window.event.keyCode > 122){
                            event.returnValue = false;
                       } else if (window.event.keyCode == 32){
                            window.event.keyCode = 95;
                       }
                       break;
                 case 'url':
                       if ((window.event.keyCode < 47 && (window.event.keyCode != 13 && window.event.keyCode != 45 && window.event.keyCode != 46 && window.event.keyCode != 38)) || (window.event.keyCode > 57 && window.event.keyCode < 97 && window.event.keyCode != 95 && window.event.keyCode != 61 && window.event.keyCode != 63 && window.event.keyCode != 64) || window.event.keyCode > 122){
                            event.returnValue = false;
                       }
                       break;
                 default: //não permite a tecla enter
                       if (event.keyCode == 32){ event.returnValue = false;}
                       break;
         }
}

</SCRIPT>

<input type="text" onKeyPress="teclas('numerico')" name="textfield" />

Muito bem isto funciona mas em cima aparece sempre a seguinte mensagem: To help protect your security, internet Explorer has restrrited this webpage from running scripts or Activx controls that could acess your computer.

Não á maneira de que não apareça isto?

Link to comment
Share on other sites

isso so aparece quando corres na maquina local, e com o ie, sempre que poes js, activex ou flash

se publicares a pagina, ou abrires com o firefox, ja nao aparece

Proud LEIC-A@IST student!

Link to comment
Share on other sites

Boas.

Aqui tens o código para fazeres o que queres.

function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

Para chamares isto fazes:

onkeyup="IsNumeric(this.value)"

Espero que te sirva.

Cumps

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.