Text only Input javascript

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;
        }

    }

Right, you're blocking numbers, but, you should also block keycodes above 57 and you're not :( That is, I want to be able to type only from small 'A to Z'

What's wrong?

 onKeyPress="return bloqueiaNumero(event);"
    
asked by anonymous 08.06.2016 / 21:56

5 answers

1

Boss uses regex, legal funfa 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>

See yourself enjoying yourself ... Ai adapts ..

    
08.06.2016 / 23:40
0

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

if ((tecla >= 48 && tecla <= 57) || (tecla === 8))
    
08.06.2016 / 22:40
0

What I understood was that you want only letters "a" through "z" (only tiny) to be typed. Their KeyCode ranges from 97 to 117.

I have refined your code and it looks 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>

One note, you remember Backspace, but you do not need to use the spacebar in your fields? If so, remember to add another || for the 32 keyCode.

God Bless Us!

    
08.06.2016 / 22:55
0

"I want to be able to type only 'A to Z' minuscule ..."

If you just want tiny 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 in the imput what you requested, tiny characters. Here is the example functional.

    
09.06.2016 / 15:17
0

Just like here you want to limit the characters that are typed.

I've tailored the code that was being used in the other response to fit your problem:

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

    if (!regex.test(key)){
        event.preventDefault();
        return false;
    }
}
]+$"); 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 lowercase letters (az) as you requested, (\ b) backspace, white space, and (\ 0) any key that has null return, ie any key that does not print something on the screen, backspace, tab, arrows and so on.

    
09.06.2016 / 15:31