I've been searching for a lot of functions that let me block the user from writing spaces on my input BUT I can't seem anyone that actually works with me.. Maybe I'm not just doing it the right way and I'd appreciate a little help.
This is my HTML form:
<table>
<form name="form_registar" method="post" action="registado.php" onsubmit="return validateForm()">
<tr>
<td colspan="2" align="center">
<input type="text" name="user" placeholder="Nome de Utilizador" id="textboxEmail" maxlength="20">
</td>
</tr>
<tr>
<td>
<input type="password" name="pw" placeholder="Password" id="textbox" maxlength="15">
</td>
<td>
<input type="password" name="cpw" placeholder="Confirmar Password" id="textbox" maxlength="15">
</td>
</tr>
<tr>
<td>
<input type="text" name="nome" placeholder="Nome" id="textbox" onkeypress="return isLetter(event)" maxlength="15">
</td>
<td>
<input type="text" name="apelido" placeholder="Apelido" id="textbox" onkeypress="return isLetter(event)" maxlength="15">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="text" name="email" placeholder="E-mail" id="textboxEmail" maxlength="50">
</td>
</tr>
<tr>
<td>
Data de Nascimento:
</td>
<td>
<input type="date" name="dataNasc" id="textbox">
</td>
</tr>
<tr>
<td>
Masculino<input type="radio" name="sex" value="M" checked>
</td>
<td>
Feminino<input type="radio" name="sex" value="F">
</td>
</tr>
<tr>
<td>
<button type="submit" class="submit-button"><b>Registar</b></button>
</td>
</form>
<form action="index.html">
<td>
<button type="submit" class="submit-button">Cancelar</button>
</td>
</form>
</tr>
</table>
And I want to block the user to write spaces in one or more input fields. I can't sem to know how.
EDIT: isLetter and isNumber are functions to only allow the user to write letters and numbers respectively.
ADDITIONAL INFO: This is my javascript functions, I'm looking for one that blocks the user from writing spaces:
/* APENAS PERMITE NUMEROS */
function isNumber(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
}
/* APENAS PERMITE LETRAS */
function isLetter(evt) {
evt = (evt) ? evt : event;
var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
((evt.which) ? evt.which : 0));
if (charCode > 31 && (charCode < 65 || charCode > 90) &&
(charCode < 97 || charCode > 122)) {
return false;
}
return true;
}
/* VALIDAR O FORM */
$(function chora() {
$('#user').on('keypress', function chora(e) {
if (e.which == 32)
return false;
});
});
function validateForm() {
/* USERNAME */
var a = document.forms["form_registar"]["user"].value;
if (a == null || a == "") {
alert("O Username não está preenchido.");
return false;
}
/* PASSWORD */
var b = document.forms["form_registar"]["pw"].value;
if (b == null || b == "") {
alert("A password não está preenchida.");
return false;
}
/* CONFIRMAR PASSWORD */
var c = document.forms["form_registar"]["cpw"].value;
if (c == null || c == "") {
alert("A confirmação de password não está preenchida.");
return false;
}
/* NOME */
var d = document.forms["form_registar"]["nome"].value;
if (d == null || d == "") {
alert("O nome não está preenchido.");
return false;
}
/* APELIDO */
var e = document.forms["form_registar"]["apelido"].value;
if (e == null || e == "") {
alert("O apelido não está preenchido.");
return false;
}
/* EMAIL */
var f = document.forms["form_registar"]["email"].value;
var atpos = f.indexOf("@");
var dotpos = f.lastIndexOf(".");
if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= f.length) {
alert("O E-mail não se encontra devidamente preenchido.");
return false;
}
/* DATANASC */
var g = document.forms["form_registar"]["dataNasc"].value;
if (g == null || g == "") {
alert("A Data de Nascimento não está preenchida.");
return false;
}
/* CONFIRMAÇAO PW*/
var h = document.forms["form_registar"]["pw"].value;
var i = document.forms["form_registar"]["cpw"].value;
if (h != i) {
alert("As passwords não correspondem.");
return false;
}
}