First, I apologize if the question is very silly since I do not handle much of the subject. I'm doing this script and I test it with lint javascript I throw 2 warning that could improve
1 Unnecessary 'else' after disruption. } else {
2 Function statements should not be placed in blocks.Use a function expression or move the statement to the top of the outer function.
How could you correct those errors or at least the second. Thank you very much for your help.
var productMainCategoryId = $('#productMainCategoryId').val();
if (productMainCategoryId == 23 || productMainCategoryId == 27 || productMainCategoryId == 28) {
$("small.float-xs-right").text('Limite 20 caracteres');
function validar() {
//Almacenamos los valores
nombre = $('.product-message').val();
//Comprobamos la longitud de caracteres
if (nombre.length < 20) {
alert('La formula funciono. texto agregado');
return true;
} else {
$("small.float-xs-right").html("<span style='color: #ff0000;'>Se excede el limite permitido</span>");
alert('La formula funciono, pero te pasaste de los caracteres');
return false;
}
}
} else if (productMainCategoryId == 29 || productMainCategoryId == 30 || productMainCategoryId == 31) {
$("small.float-xs-right").text('Limite 18 caracteres');
function validar() {
//Almacenamos los valores
nombre = $('.product-message').val();
//Comprobamos la longitud de caracteres
if (nombre.length < 18) {
alert('La formula funciono con 18. texto agregado');
return true;
} else {
$("small.float-xs-right").html("<span style='color: #ff0000;'>Se excede el limite permitido</span>");
alert('La formula funciono, pero te pasaste de los caracteres');
return false;
}
}
} else if (productMainCategoryId == 26) {
$("small.float-xs-right").text('Limite 18 caracteres');
function validar() {
//Almacenamos los valores
nombre = $('.product-message').val();
//Comprobamos la longitud de caracteres
if (nombre.length < 25) {
alert('La formula funciono con 25. texto agregado');
return true;
} else {
$("small.float-xs-right").html("<span style='color: #ff0000;'>Se excede el limite permitido</span>");
alert('La formula funciono, pero te pasaste de los caracteres');
return false;
}
}
} else if (productMainCategoryId == 24) { //Classic II 15 carac
$("small.float-xs-right").text('Limite 18 caracteres');
function validar() {
//Almacenamos los valores
nombre = $('.product-message').val();
//Comprobamos la longitud de caracteres
if (nombre.length < 15) {
alert('La formula funciono con 15. texto agregado');
return true;
} else {
$("small.float-xs-right").html("<span style='color: #ff0000;'>Se excede el limite permitido</span>");
alert('La formula funciono, pero te pasaste de los caracteres');
return false;
}
}
} else {
alert('El Id de la página actual es diferente al ID del la formula');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<form method="post" action="" onSubmit="return validar();">
<div>
<label> Marcado</label>
<p></p>
<input type="hidden" id="productMainCategoryId" value="29">
<textarea placeholder="marca aca" class="product-message" maxlength="250" name="marcado"></textarea><br>
<small class="float-xs-right">Máximo 200 caracteres</small>
<p></p>
<button class="btn btn-primary float-xs-right" type="submit" name="submitCustomizedData">guardar</button>
</div>
</form>
Edit, Take out the conditional function
var productMainCategoryId = $('#productMainCategoryId').val();
function validar() {
//Almacenamos los valores
limiteCaracter = $('.product-message').val();
if (productMainCategoryId == 23 || productMainCategoryId == 27 || productMainCategoryId == 28) {
$("small.float-xs-right").text('Limite 20 caracteres');
//Comprobamos la longitud de caracteres
if (limiteCaracter.length < 20) {
alert('La formula funciono. texto agregado');
return true;
} else {
$("small.float-xs-right").html("<span style='color: #ff0000;'>Se excede el limite permitido</span>");
alert('La formula funciono, pero te pasaste de los caracteres');
return false;
}
}
}
New Code
var productMainCategoryId = $('#productMainCategoryId').val();
function validar(largo, limite) {
if (largo < limite) {
alert('La formula funciono con ' + largo + '. texto agregado');
return true;
}
else
{
$("small.float-xs-right").html("<span style='color: #ff0000;'>Se excede el limite permitido</span>");
alert('La formula funciono, pero te pasaste de los caracteres');
return false;
}
}
if ( productMainCategoryId == 23
|| productMainCategoryId == 27
|| productMainCategoryId == 28) {
$("small.float-xs-right").text('Limite 20 caracteres');
//Almacenamos los valores
nombre = $('.product-message').val();
validar(nombre.length, 20);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<form method="post" action="" onSubmit="return validar();">
<div>
<label> Marcado</label>
<p></p>
<input type="hidden" id="productMainCategoryId" value="23"> <!-- El ID CAMBIA DEPENDENDO DE LA CATEGORIA-->
<textarea placeholder="marca aca" class="product-message" maxlength="250" name="marcado"></textarea>
<br>
<small class="float-xs-right">Máximo 200 caracteres</small>
<p></p>
<button class="btn btn-primary float-xs-right" type="submit" name="submitCustomizedData">guardar</button>
</div>
</form>
<P> al abrir la página aparece un alert "La formula funciono con 0. texto agregado" y al poner 1 solo caracter y le doy guardar dice que me pase :( </P>