I am creating a small community blog and I would like each new user to have a nickname that begins with a capital letter, followed by at least two lowercase letters, followed by at least one number and I want to do the checks all first with javascript then with php.
For javascript checks, I would like this to happen when typing in the field (event input) and if the nickname does not match the rules the border of the input field turns red as in most websites.
For that, I have a function that deals with the coloring and that takes two parameters and another that checks if the entered data correspond to the regex it contains. The latter takes a parameter too. But my code does not work as I would have liked. Please guide me.
function surligne(champ, erreur)
{
if(erreur)
champ.style.backgroundColor = "red";
else
champ.style.backgroundColor = "";
}
function verifPseudo(champ)
{
var regex= /^[A-Z][a-z]{2,}[0-9]+/;
if(!regex.test(champ.value))
{
surligne(champ, true);
return false;
}
else
{
surligne(champ, false);
return true;
}
}
var pseudoElt=document.getElementById("pseudo");
pseudoElt.addEventListener("input", verifPseudo(this));
In another forum, someone told me that the problem comes from my function in the addEventListenener