In the manner below it will verify whether something was written by means of the regular expression /\w/
, ignoring the white spaces.
Edited example:
$("#buscar").on("blur", function(e) {
verify(e);
});
$("#buscar").on("keypress", function(e) {
if (e.keyCode == 13) {
verify(e);
}
});
function verify(e) {
if (!(/\w/.test($("#buscar").val()))) {
e.preventDefault();
$('p').html('Preencha algo!');
} else {
$('p').html('');
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form action="">
<input type="text" id="buscar">
</form>
<p></p>
I used the function Blur() which is triggered by the user to remove the focus from the input
. But if you prefer the click
, just put .on("click",
.
yes I have the part in php that returns what write in this input you want me to post?
– Leonardo Costa
I’m not doing this part by ajax I just wanted to check not to let it go blank just that the rest can stay the way it is
– Leonardo Costa
like I know how to check but with a boot without the boot so I don’t know what kind to force a click without the boot
– Leonardo Costa