I'm trying to check if the keypress is number or "forward slash" and if its not create tooltip that will show "Letters allowed only!" but when the function starts to create the tooltip its not going to return false;
. Also when it creates the tooltip he is on the top left corner of the page not on the input position. How can I fix this?
$(document).ready(function() {
$('.groupOfTexbox').keypress(function(event) {
return isNumber(event)
});
var $tooltip = $('<div style="display:none;position:absolute;padding:10px;background:rgba(0,0,0,0.5);"></div>').appendTo(document.body);
function isNumber(evt) {
var charCode = evt.which;
if ((charCode != 47) && (charCode < 48 || charCode > 57)) {
$tooltip.text($(this).data('tooltip'));
$tooltip.show().css({
top: 0,
left: 0
});
var test = $('groupOfTexbox');
var calculator = new $.PositionCalculator({
item: $tooltip,
itemAt: "bottom left",
target: test,
targetAt: "top left",
flip: "both"
});
var posResult = calculator.calculate();
$tooltip.css({
top: posResult.moveBy.y + "px",
left: posResult.moveBy.x + "px"
});
return false;
} else {
return true;
}
}
});
<input type="text" class="groupOfTexbox" data-tooltip="Hello" />