Here is a small example
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<body>
<form id="api_crud_form" name="api_crud_form" action="https://www.google.es">
Nombre: <input type="text" name="nombre" id="nombre"><br>
<input type="submit" value="Enviar">
</form>
</body>
</html>
<script type="text/javascript">
$(document).ready(function () {
$('#api_crud_form').on('submit', function (event) {
event.preventDefault();
if ($('#nombre').val() == '') {
alert("Ingresar Nombre");
}
else{
alert("se envio los datos");
}
});
});
</script>
Here is a small example
It is an example that validates if it is empty but does not validate if insert empty spaces
How can I validate empty spaces?
I'm sure you mean to validate that the input is not empty and does not contain whitespace. For that, it occupies the javascript function
trim
that basically what it does is clean a string of spaces on the left and right, this allows you to clean text strings of spaces.Also, we will use
length
to count the characters of the string collectedjQuery
from the input in question:Example:
I tested it using jQuery 1.2
What you are looking for is perfect and shorter with pure JS in this way:
Form
JAVA SCRIPT
What we do is:
input
send and first verify if the value of the propertyvalue
is equal to""
which indicates that the user did not write and then notify accordinglytrim()
chaining it to the name of the variable with which we obtained the oneinput
where the user wroteReference
trim()