Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

212
Vistas
How to alert under input text with conditions?

Please look the picture here

enter image description here

want to divided the alert. So when value is empty or null, the alert is nama pengguna must inputed. And if value is less then 5, the alert is min 5 characters.

For the style is only like it (red text, under input text). I mean not use javascript alert.

This is my code:

function checkLength() {
	var textbox = document.getElementById("username");
	if(textbox.value.length <= 5) {
		alert("min 5 characters");
	}
	else if(textbox.value.length == 0){
		alert("nama pengguna must inputed")
	}
}
<div class="form-group required2">
	<label class="control-label visible-ie8 visible-ie9">Nama Pengguna</label>
	<div class="input-icon">
 		<i class="fa fa-user"></i>
		<input id="username" class="form-control placeholder-no-fix" pattern=".{5,}" type="text" autocomplete="off" placeholder="Nama Pengguna" name="username" onchange="checkLength()" required title="nama pengguna must inputed (min 5 characters)">
	</div>
</div>

Thanks.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You could create a p tag and append those errors as suggested i.e. characters length should be less then 5, as below, I have even made few correction in conditions.

var textbox = document.getElementById("username");
var pr = document.createElement("p");
function nm(){
	if(textbox.value.length >= 1 && textbox.value.length <= 5){
  pr.textContent = "min 5 characters";
  document.body.appendChild(pr);
  }
  else if(textbox.value.length === 0){
  pr.textContent = "nama pengguna must inputed";
  document.body.appendChild(pr);  
  }
}
textbox.addEventListener("blur",nm);
p{
  color:red;
}
<div class="form-group required2">
  <label class="control-label visible-ie8 visible-ie9">Nama Pengguna</label>
  <div class="input-icon">
    <i class="fa fa-user"></i>

    <input id="username" class="form-control placeholder-no-fix" pattern=".{5,}" type="text" autocomplete="off" placeholder="Nama Pengguna" name="username" required title="nama pengguna must inputed (min 5 characters)" />
  </div>
</div>

over 4 years ago · Santiago Trujillo Denunciar

0

If you want to change your message accordingly, you will be battling against HTML5 validation, but that won’t be too big a problem.

What you need to do is to check the value when the data has changed.

Put the following into your stattup code:

document.querySelector('input#username').onchange=function() {
    checkLength();
}

By startup code, I mean whatever you use to start your script. Tuypically it is something like:

document.addEventListener('DOMContentLoad',init,false);
function init() {

}

or, for older browsers,

window.onload=init;
over 4 years ago · Santiago Trujillo Denunciar

0

If you want convert alert into simple text message then follow the code below.

HTML

<div class="form-group required2">
        <label class="control-label visible-ie8 visible-ie9">Nama Pengguna</label>
        <div class="input-icon">
            <i class="fa fa-user"></i>

            <input id="username" class="form-control placeholder-no-fix" pattern=".{5,}" type="text" autocomplete="off" placeholder="Nama Pengguna" name="username" required title="nama pengguna must inputed (min 5 characters)" />
<span class="username_error" class="error"></span>
</div>  
    </div>

Javascript

<script>
    function checkLength(){
        var textbox = document.getElementById("username");
        if(textbox.value.length <= 5){
            document.getElementById('username_error').innerHTML = "min 5 characters";
        }
        else(textbox.value.length == 0){
            document.getElementById('username_error').innerHTML = "nama pengguna must inputed";
        }
    }
</script>

CSS

<style>
.error{
  color:"red";
}
</style>
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda