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>
Have you seen str_pad? https://secure.php.net/manual/en/function.str-pad.php
– Velasco
Can you clarify your problem better? Which character will it be? Why does it need to be inserted? What is its function?
– Woss
I’ll add the question
– Felipe Henrique
@Andersoncarloswoss rephrased the question I think it’s now clearer what I need
– Felipe Henrique
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.
– Woss
@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
– Felipe Henrique
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
– Wendel Rodrigues
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.
– Woss
@Wendelrodrigues man if that’s what I love you I’ll test it
– Felipe Henrique
@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?
– Woss
@Andersoncarloswoss, would you tell me how I do the code that enters the adiconal number? or some documentation I believe via JS is easier
– Felipe Henrique
@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.
– Wendel Rodrigues