Well, I made an example in JsFiddle: link
You will notice that there are two text boxes. Basically, the system will check for a difference of more 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>
I made it in pure javascript anyway.
The property substring
, works by capturing the character of String
, for example:
var ex = "Exemplo";
ex.substring(0, 3);
It would result in:
Exe
It counts as if it works just like a selection:
| E | x | e | m | p | l | o |
In the above case, he selected from the first bar (0) to the fourth (3).
See more about subtring
here: link
There is also substr
, explained here: link