validate text box so that first character is a letter [closed]

0

I have a php form for a user record, and I'm validating the input type text with javascript

Is there a function in javascript that makes the first character to be entered in a text box be a letter? for now I have validation to text boxes only letters and only numbers, also that the first letter to be entered is capitalized so the user places it in mini, but I would like to validate a text box that when you are going to write, it is mandatory at the beginning start with a letter and then if they can be numbers and so on. Can you?

for the validation of numbers and letters I did the following:

function soloLetras(e) {
    key = e.keyCode || e.which;
    tecla = String.fromCharCode(key).toString();
    letras = " áéíóúabcdefghijklmnñopqrstuvwxyzÁÉÍÓÚABCDEFGHIJKLMNÑOPQRSTUVWXYZ";//Se define todo el abecedario que se quiere que se muestre.
    especiales = [8, 9, 37, 39, 46, 6]; //Es la validación del KeyCodes, que teclas recibe el campo de texto.

    tecla_especial = false
    for(var i in especiales) {
        if(key == especiales[i]) {
            tecla_especial = true;
            break;
        }
    }

    if(letras.indexOf(tecla) == -1 && !tecla_especial){
alert('Este campo solo permite letras');
        return false;
      }
}


function SoloNumeros(evt){
 if(window.event){//asignamos el valor de la tecla a keynum
  keynum = evt.keyCode; //IE
 }
 else{
  keynum = evt.which; //FF
 } 
 //comprobamos si se encuentra en el rango numérico y que teclas no recibirá.
 if((keynum > 47 && keynum < 58) || keynum == 8 || keynum == 13 || keynum == 6 ){
  return true;
 }
 else{
  return false;
 }
}

and to apply it in the text boxes, always add the following to the end of the required:

SI ES LETRAS ---> required onkeypress="return soloLetras(event);" onkeyup="this.value = this.value.charAt(0).toUpperCase() + this.value.slice(1);" class="mayusculas" maxlength="15"  

  SI ES NUMEROS--->  onKeyPress="return SoloNumeros(event);"

Now as I want to place a field "address", I want the user to enter it is always a letter at the beginning and not a number, so my query.

    
asked by rodrigo2324 17.08.2017 в 16:16
source

2 answers

2

I think this is what you are looking for, what it does is to count how many characters the input has, if it is empty it allows to put letters, if it is already greater than 1 it lets you enter numbers.

I hope you serve, greetings from Honduras.

  function soloLetras(e) {
    textoArea = document.getElementById("cajaTexto").value;
    var total = textoArea.length;
    if (total == 0) {
      key = e.keyCode || e.which;
      tecla = String.fromCharCode(key).toString();
      letras = " áéíóúabcdefghijklmnñopqrstuvwxyzÁÉÍÓÚABCDEFGHIJKLMNÑOPQRSTUVWXYZ"; //Se define todo el abecedario que se quiere que se muestre.
      especiales = [8, 9, 37, 39, 46, 6]; //Es la validación del KeyCodes, que teclas recibe el campo de texto.

      tecla_especial = false
      for (var i in especiales) {
        if (key == especiales[i]) {
          tecla_especial = true;
          break;
        }
      }

      if (letras.indexOf(tecla) == -1 && !tecla_especial) {
        return false;
        alert('No puedes comenzar escribiendo numeros');
      }
    }
  }

function SoloNumeros(evt) {
  if (window.event) { //asignamos el valor de la tecla a keynum
    keynum = evt.keyCode; //IE
  } else {
    keynum = evt.which; //FF
  }
  //comprobamos si se encuentra en el rango numérico y que teclas no recibirá.
  if ((keynum > 47 && keynum < 58) || keynum == 8 || keynum == 13 || keynum == 6) {
    return true;
  } else {
    return false;
  }
}
<label>Letras</label> <input type="text" id="cajaTexto" required onkeypress="return soloLetras(event);" onkeyup="this.value = this.value.charAt(0).toUpperCase() + this.value.slice(1);" class="mayusculas" maxlength="15" ">

   <label>Numeros</label> <input onKeyPress="return SoloNumeros(event); "/>
    
answered by 17.08.2017 / 17:42
source
1

function check(){
var letra = /[a-z]/;
var d = document.getElementById("d").value;
var s = d.split("");  
if(letra.test(s[0]) == 0) console.info("No es letra");
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<input id="d" type="text" placeholder="Direccion"/>
<input type="button" value="Chequear" onclick="check()"/>
  </body>
</html>

With a regular expression that regulates if it is between a-z, you can do it.

    
answered by 17.08.2017 в 16:44