Well, I did an example on Jsfiddle: http://jsfiddle.net/SamirChaves/y41q39yd/4/
You will notice that there are two text boxes. Basically, the system will check if there is a difference greater than 3 characters.
function verifyChange(){
var span = document.getElementsByTagName("span")[0];
var name = document.getElementById("name_user");
var correcao = document.getElementById("correcao_name_user");
var char1 = []; //quantidade de caracteres do name_user
var char2 = []; //quantidade de caracteres do correcao_name_user
for(var i = 0; i < name.value.length; i++){
char1[i] = name.value.substring(i, i+1);
}
for(var j = 0; j < correcao.value.length; j++){
char2[j] = correcao.value.substring(j, j+1);
}
if(char1.length - char2.length > 3 || char2.length - char1.length > 3){
span.innerHTML = "Modificação inválida.";
}else{
var NCrt = 0; //nº de caracteres modificados
var n = 0;
if(char1.length == char2.length){
for(var i = 0; i < char1.length; i++){
if(char1[i] != char2[i]){
NCrt++;
}
}
}else{
if ( char1.length < char2.length){
Mlength = char1.length
for(var j = 0; i < char2.length; i++){
for(var i = 0; i < char1.length; i++){
if(char1[j] != char2[i]){
NCrt++;
}
}
if(NCrt == char2.length){
n++;
}
}
}else {
for(var j = 0; i < char1.length; i++){
for(var i = 0; i < char2.length; i++){
if(char2[j] != char1[i]){
NCrt++;
}
}
if(NCrt == char1.length){
n++;
}
}
}
}
if(NCrt > 3 || n > 3){
span.innerHTML = "Modificação inválida.";
}else{
span.innerHTML = "Modificação válida.";
}
}
}
<form action="javascript: verifyChange()">
<input type="text" onblur="verifyChange()" id="name_user" value="George">
<input type="text" onblur="verifyChange()" id="correcao_name_user" value="gege">
<input type="submit" value="Verificar" onclick="verifyChange()">
<br>
<br>
<span></span>
</form>
Fiz em javascript puro mesmo.
The estate substring
, works by capturing the character of String
, for example:
var ex = "Exemplo";
ex.substring(0, 3);
Would result in:
Exe
It counts as if it worked equal to a selection,
|E|x|e|m|p|l|o|
in the above case, it selected from the first bar (0) to the fourth (3).
See more about the subtring
here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring
There is also the substr
, explained here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr
I think I get the whole character change thing. For example, if the user fills the field with "George" and then switches to "Geor", it will be possible because only 2 characters, "g" and "e" have been changed. Is that right? But there’s something I don’t quite understand, so the hidden input?
– Samir Braga
In the hidden input is the name in the format in which it came from the comic book. Ai whenever it leaves the field Onblur sends the value of the hidden input and the visible input where the changes were made.
– George Wurthmann
@Georgewurthmann please post the code you have already made and if possible create some examples that could help us understand the problem.
– Gabriel Rodrigues
@Gabriel Rodrigues, I will be the solution indicated by Bacco. You want me to post my code anyway?
– George Wurthmann