Fixed value in a text field PHP

1

I have a form where in one of the fields I ask for a person's ID. My question is, how can I make a letter in the input text that is necessary for my registration of the DNI field? I mean, I have the following code:

<div class="form-group" style="padding: 20px">

       <select name="c" id="c" onchange="if(this.value!='-') cedula.value=this.value;">
      <option value="-" selected="selected">Elige</option>
      <option value="V"> V </option>
      <option value="E"> E </option>
    </select>




      <label for="nombres" class="col-md-3 control-label">Cedula:<span class="asterisco">*</span>  </label>
        <div class="col-md-8">

  <input type="text" id="ced1" name="cedula" value="<?php if(isset($cedula)) echo $cedula; ?>" /> 


        </div>
     </div>

When selecting an option of the comboBox (V, E ..) the text box is filled with that letter, but that letter can easily be deleted by hitting the keyboard key of the pc, I would like it not to be possible delete but I do not see the form.

Try applying masks to the text box as follows:

<script type="text/javascript">
        jQuery(function($){
            $("#numero1").mask("9,99", {

                // Generamos un evento en el momento que se rellena
                completed:function(){
                    $("#numero1").addClass("ok")
                }
            });

            // Definimos las mascaras para cada input
           // $("#date").mask("99/99/9999");
            $("#movil").mask("(9999)-999-99-99");
           // $("#letras").mask("aaaaaaaaaaaaaa");
            $("#ced1").mask("V-9999999");
             $("#ced2").mask("E-");
              $("#ced3").mask("J-");
           // $("#comodines").mask("?");
           $("#letras1").mask('aaaaaaaaaaaaaa');
        });   </script>

but it does not work for me since it is a single text box and if I apply a single mask for a single letter (The V for example) I can not place the E ... if they could help me I would appreciate it.

The letters signify the nationality of the person registering, in this case V- is for Venezuela, and E- for which he is a foreigner.

    
asked by rodrigo2324 16.08.2017 в 04:46
source

4 answers

2

If you are always in the same position, I do not see why you want to complicate mixing the content of the input with the letter. You can do it like this:

You put a select with the possible letters, and next to your normal input, that accepts the other DNI values. Then you can obtain the DNI by combining both values.

I think it's an elegant way and you can more easily control the contents of numbers on the DNI etc.

If you want, using CSS you can have two different elements and make them appear as if they are together :) As you can see, it is much more comfortable and simple to use them as separate elements that you can combine according to your need.

I have linked the collection of the value to the event onclick of a button, but you can link it to the event onchange of the select or any other ...

As you have considered the solution is possible, but you would have to go through CSS, add and remove the letters of the input every time a onchange occurs in the select, etc. And they would always be two different elements, since the letter would have to be stored in a div, a span or another element which you would put as next to the input by CSS.

I think there is no need. See how the code fragment works with the solution I propose:

$('#btnEnviar').click(function() {
  laLetra = $('#letra').val();
  elNumero = $('#numero').val();
  elDni = laLetra + elNumero;
  alert(elDni);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<select id="letra">
      <option value="V-">V-</option>
      <option value="E-">E-</option>
</select>

<input type="text" id="numero" size="15" />
<button id="btnEnviar">Enviar</button>
    
answered by 16.08.2017 / 06:18
source
0

It is not necessary to complicate, add that letter to the side of where you need to enter the text.

[LETTER] [INPUT TEXT]

Only with the JavaScript you have to "update" the letter according to your convenience. You can use a:

<span id="letra"></span>

And manipulate it from there.

    
answered by 16.08.2017 в 04:52
0

How about like that?

function evaluarContenido(cadenaIngresada){
  var evaluarCadena = cadenaIngresada.split("");
  if(evaluarCadena[0] != '-'){
    $("#DNI").val("-"+cadenaIngresada);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="DNI" value="" onchange="evaluarContenido(this.value)">

Of course, this is just an example and you can modify it to your needs.

    
answered by 16.08.2017 в 05:25
0

Do not you think you could do it with the property readonly="readonly"? Or do you have to put the letter in the input and be able to edit that input?

    
answered by 16.08.2017 в 05:25