Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Marcelo Retana
2,534 PointsI need to use setCustomValidity() to set up my own alerts
Hi you all, I need to set up my own alerts with setCustomValidity() because I am using HTML5, I've been trying on many different ways but I know I am failing with the JavaScript logic, look at my code please and help me as much as you can.
<form role="form" method="POST" id="productForm" action="/products">
<div class="col-sm-8 no-padding prod">
<label>Nombre del producto/servicio</label>
</div>
<div class="col-sm-8 no-padding prod">
<div class="form-group">
<input type="text" class="form-control pro-template-input" id="prod-input-name" name="prod[name]" placeholder="Nombre" required>
</div>
</div>
<div class="col-sm-4 prod no-padding">
<input type="hidden" name="prod[productType]" id="selectedType" required>
<div class="dropdown full-width-dropdown">
<button name="prod[productType]" class="btn btn-default dropdown-toggle dropdown-prod" type="button" id="type-prod" data-toggle="dropdown">
<span>Tipo</span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
<li role="presentation"><a data-value="product" role="menuitem" tabindex="-1" href="#">Producto</a></li>
<li role="presentation"><a data-value="service" role="menuitem" tabindex="-1" href="#">Servicio</a></li>
</ul>
</div>
</div>
<div class="row prod-clear no-padding">
<div class="col-sm-4 prod">
<div class="form-group prod modal-clear-spacer">
<input type="text" class="form-control pro-template-input double" id="prod-price" name="prod[productPrice]" placeholder="Precio" required>
</div>
</div>
</div>
<div class="form-group" id="descrip-prod-add">
<label>Descripción</label>
<textarea class="form-control prod-textarea pro-template-input" rows="3" name="prod[productDescription]" required></textarea>
</div>
<!--tipo promocion-->
<div class="form-group modal-clear-spacer">
<label>Palabras Clave</label>
<input type="text" class="form-control pro-template-input" id="prod-keyword" name="prod[productKeywords]" placeholder="Palabras claves" required/>
</div>
and here is de JavaScript:
(function(){
var productsValidity = {
initialize: function(){
this.bindEvents();
},
bindEvents: function(){
$('input').click(this.validateProductsInputs);
},
validateProductsInputs: function () {
var inputs = $('input');
if (!(inputs.val() === '')) {
this.setCustomValidity('');
}else {
this.setCustomValidity('Debes llenar este espacio');
};
}
};
productsValidity.initialize();
})();