I would like to make a validation of a form. More precisely, I need to ask for my user to fill the form with the following conditions:
- email: it must have letters and one @,
- password: it must have letters, numbers and only lower letters. How can I ask it to my users in JS?
I have here the code for "name" but I don't know which method use to others:
let nome = document.querySelector('#name')
nome.addEventListener('keyup', () =>
{if(nome.value.length <= 5)
{nome.setAttribute('style', 'border-color: red')}
else
{nome.setAttribute('style', 'border-color: green')}});
Indeed I have been using, now, this code here. These are the variables:
let nome = document.getElementById('nome');
let nomeCombinaRegex = nome.value.match(/^((\b[A-zÀ-ú']{3,40}\b)\s*){2,}$/);
let nomeVerde = nome.setAttribute('style', 'color:green');
let nomeRed = nome.setAttribute('style', 'color:red');
This is the HTML:
<div class="form">
<i class="bi bi-person"></i>
<input id="nome" type="text" required autofocus placeholder="Nome" oninput="nameValidate()">
</div>
This is the JS Function:
function nameValidate()
{if(nomeCombinaRegex)
{nomeVerde}
else
{nomeAlert;
nomeRed}}
Question: What am I doing wrong? the code doesn't work (I'm new in JS)