Add a character dynamically with PHP or Javascript

Asked

Viewed 105 times

0

Is there a PHP or Javascript function that adds a character (in the case of a number) when a field has only 8 digits. within a input. because I have a form where there is the RG field, and the RG is composed of 9 digits but there are some users who have a digit less and would like that when the user type the RG field with only 8 digits he save in the bank with an extra digit that would be the 0

Remembering that everything is working normal on my site just really like that when the user type the RG with a digit less he save with this 0

Link to website

  • 1

    Have you seen str_pad? https://secure.php.net/manual/en/function.str-pad.php

  • Can you clarify your problem better? Which character will it be? Why does it need to be inserted? What is its function?

  • I’ll add the question

  • @Andersoncarloswoss rephrased the question I think it’s now clearer what I need

  • 1

    I’m sorry to be so blunt, but this seems like a scam. Do you want to change the user’s ID? Then how will you know if the user originally typed 8 characters or if it really is 0 at that position? You shouldn’t have to make this kind of change for your system to work.

  • @Andersoncarloswoss users with the missing digit are not being able to complete the purchases and I believe that adding a 0 or an x in the case solves the problem because I have seen how much one digit less in the RG and added an x or a 0 so I think it is valid to do so if you can help me in this I am very grateful I was not in who developed the site and this was the only solution that I could think of for the moment because I don’t know anything about opencart and I can not make a very complex change to not compromise the system

  • The problem is that in the bank you must have used CHAR(9), just put VARCHAR(9), so it would accept both 8 and 9 characters without the need of gambiarra. rs

  • In fact the X is used, the 0 I can not say. If you have an official source that says you can use 0, ok, you can do it, but still you’d better change your system so it only accepts the 8 digits.

  • @Wendelrodrigues man if that’s what I love you I’ll test it

  • @Wendelrodrigues but CHAR would also accept 8 characters, only in memory the database would fill up to complete the 9 positions. The difference is only in memory occupation, no?

  • 1

    @Andersoncarloswoss, would you tell me how I do the code that enters the adiconal number? or some documentation I believe via JS is easier

  • @Andersoncarloswoss is true, I floated here. I’m sorry. Kirito the answer I gave I think it solves, just take the user’s ID, if you only have 8 you add a 0 and store in the bank.

Show 7 more comments

3 answers

3

Has the function str_pad:

<?php
print str_pad('45', 10, "0", STR_PAD_LEFT);  // Gera "0000000045"
print str_pad('23', 10, "0", STR_PAD_BOTH);  // Gera "0000230000"
print str_pad('76', 10, "0");  // Gera "6700000000"
  • thanks for the answer I need to add this in a specific field in case a <input> in the form would look like this

  • i added this in my php and it works more precise to make it work when the user type in input a rg smaller than 9 digits so it adds a 0 at the end need not display for the user more display has no problem the idea and when it registers it appears in panel already with type 0 inserted if the user has only registered with 8 digits for example

  • Yeah, but he shows that panel only after he signs in, right? If you add 0 in the register when it goes to the panel should already show with 0.

  • not friend is because and so the user registers on the site that was made with opencart this data goes to the database and are displayed in the administrative panel and there if the guy has registered the RG with 8 digits appears only 8 if registered with 9 the problem appears and when the user register the RG with 8 digits it can not make the purchase pq error te with 9 so I want that when it registers with a RG with less than 9 digits the system adds only a 0 at the end to be able to stay with 9 digits

  • so I saw the code you sent works more precisely to link it to my input that and where the user type RG <input type="text" name="custom_field[Account][2]" value="" placeholder="RG" id="input-custom-field2" class="form-control"> which is this guy there when he registers the system already adds 0 at the end of the RG if it is less than 9

3

As commented, the best would be to even adapt your system to accept the value of the original user RG, with 8 characters. But in fact it is possible to add a character X at the end indicating that the same does not exist - the character 0 I can’t say, if you find a reliable source to say that, okay.

With Javascript it is very quiet; just handle some event of the field or form, check the length of the data informed and add the desired character. See the example below, where I treated the event submit form.

I used return false to block the spread of the event as it is not necessary here. In your case, this section must be removed so that the event is propagated correctly.

function valida() {
  let rg = document.getElementById("rg");
  let span = document.getElementById("alert");

  if (rg.value.length < 9) {
    rg.value = rg.value + "X";
    span.innerHTML = "Seu RG foi modificado com a inserção de um X ao final por questões de compatibilidade";
  }

  return false;
}
<form action="" onsubmit="return valida();">
  <label for="rg">
    RG:
    <input type="text" id="rg" name="rg">
    <div id="alert"></div>
  </label>
  <input type="submit" value="Enviar">
</form>

  • Note: I added a notice of the value change to the user. It is much important to do this if this change is visible to the user. In this case, as I blocked the event propagation, the change will be visible. Otherwise, you can display this warning only when displaying the RG value on the system; something like if there is an X in RG, display this message ..., to make clear to the user why there is a character that he has not typed.

  • friend thanks for the answer takes me a question as my site is done in opencart and the input name is <input name="custom_field[Account][2]"> do not know how then my Let rg = Document.getElementById("rg"); should stay Let custom_field[Account][2]= Document.getElementById("rg"); but if I call it so of error there is a way to display so ? sorry manjo almost nothing opencart if you can help me thank

  • 1

    You cannot edit the field?

  • can’t edit because it loses all references with database and everything else has some way to insert this <input name="custom_field[Account][2]"> element inside the function you created

  • I have never worked with Opercart, but there is usually the option to set custom attributes to the field. You would have to read in the documentation.

  • Use this function and instead of onSubmit puts in the onBlur of the RG field: <input type="text" name="custom_field[account][2]" value="" placeholder="RG" id="input-custom-field2" class="form-control" onblur="return valida();"> Only changes the ID of the rgfor input-custom-field2

Show 1 more comment

0

I have an idea, try using Javascript. Create a javascript file put the following code:

$('.input-name').focusout(function(){
  var valor = $(this).val();
  if(valor.length < 9) $(this).val('0' + valor);   
});

After that copy the input-name name and add a class with the input-name value where you want to see this effect.

Note that this code will add the zero number as soon as the user removes the cursor from the field and if the value in the field is less than 9.

Browser other questions tagged

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