Input javascript text only

Asked

Viewed 1,479 times

0

Opa,

Very simple thing that I did not find something similar, I decided to create, I did:

    function bloqueiaNumero(texto)
    {
     var tecla = new String();

     if (window.event) {
      tecla = texto.keyCode;
     }
     else if (texto.which) {
      tecla = texto.which;
     }
     else {
      return true;
     }

        if (((tecla < 48) || (tecla > 57)) && (tecla = 8))
        {
            return true;
        }

        else
        {
          return false;
        }

    }

Okay, it’s blocking numbers, but it should also block keycodes above 57 and it’s not :( That is, I want it to be possible to type only from 'A to Z' minuscule

What’s wrong?

 onKeyPress="return bloqueiaNumero(event);"

5 answers

1

Boss uses regex, cool funnel too...

function valida_az(){
	var filter_az = /^([a-z]|\s)+$/ ;
	if(!filter_az.test(document.getElementById("input_az").value)){
	document.getElementById("input_az").value="";
	document.getElementById("input_az").value = "Somente a-z";
	document.getElementById("input_az").style.borderColor = "#ff0000";
	document.getElementById("input_az").style.outline = "#ff0000";
	document.getElementById("input_az").focus();
	document.getElementById("input_az").onkeydown = function (){
	document.getElementById("input_az").style.borderColor = "#999999";
	document.getElementById("input_az").style.outline = null;}
	}
}
<head>
	<script src="valida_az.js"></script>
</head>
<form action="" onsubmit="valida_az(this);return false;">
  <input id="input_az" type="text" onblur="valida_az()">
  <input type="submit" 	class="btn_enviar_cad"	 name="enviar_cad"  value="Enviar"  >
</form>

Get into it... get into it..

0

Hello, I believe the error is logical, in your if. Try to put it like this:

if ((tecla >= 48 && tecla <= 57) || (tecla === 8))

0

What I understood was that you wish that only letters from "a" to "z" (only lowercase) can be typed. Their Keycode goes from 97 to 117.

I redid his code and he was like this:

function bloqueiaNumero(texto){
  var tecla = texto.which || texto.keyCode;
  if ((tecla >= 97 && tecla <= 117) || (tecla === 8)){
    return true;
  }else{
    return false;
  }
}
<form action="#">
  <input type="text" onKeyPress="return bloqueiaNumero(event);">
</form>

A remark, you remembered the Backspace, but it will not be necessary to use the space key in your fields ? If it is, remember to add another || to keycode 32.

God Bless Us!

0

"I want it to be possible to type only from 'A to Z' minute"...

If you only want lowercase characters and nothing else, it would be better to use REGEX with javascript.

Try this solution:

//HTML
<input class='txt' type='text' value=''>

//Javascript
    $('.txt').bind({             

             keyup:function(){

                   var objectEvent=$(this);
                   var ck_input = /^[a-z]+$/;
                   var input = $.trim(objectEvent.val());
                   var validationTest =ck_input.test(input);

                     if(input!==""){
                       if(!validationTest){
                                objectEvent.val("");
                            alert("nao aceita");
                       } 
                   }  
             }//Fim keyUp
    });//Fim of $('.txt').bind({

It will only leave on imput what you requested, lowercase characters. Here the example functional.

0

exactly how here what you want is to limit the characters that are typed.

I adapted the code that was being used in the other answer to suit your problem:

function bloqueioNumero (event){
    var regex = new RegExp("^[a-z \b\0]+$");
    var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);

    if (!regex.test(key)){
        event.preventDefault();
        return false;
    }
}

Basically the code uses regular expressions and allows letters (a-z) minuscules as you requested, ( b) Backspace, white space and ( 0) any key that has the null return, ie any key that does not print something on the screen, Backspace, tab, arrows and etc.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.