2
I’m training JavaScript
with HTML
and CSS
and I’m creating a simple web page where I’ll do the BMI calculation. My user besides informing his weight and height,should inform his data:name,e-mail,..
Through the JavaScript
I must perform the IMC calculations and validate both the user’s name and e-mail, I have an example that I am following, but have a command that I am not understanding right : ONSUBMIT
<section class="userinfo">
<form class="info" action="index.html" method="post" onsubmit="return validateName()">
<ul>
<li><label for="nome">Nome:</label>
<input type="text" name="nome" placeholder="Digite seu nome" id="name" required/></li>
<li><label for="email">E-mail:</label>
<input type="email" name="email" placeholder="Digite seu E-mail" id="email" required></li>
<li><label for="telefone">Telefone:</label>
<input type="tel" name="telefone" placeholder="(xx)xxxxx-xxxx" id="telefone"/></li>
In the field of Form
I wouldn’t have to inform two onsubmit
? One validateName and another validateEmail?
My second question is about the functioning of JavaScript
function validateName(){
var name = document.getElementById('name').value;
var nameValidator = /^[a-záàâãéèêíïóôõöüúçñ' ]{5,}$/i;
if (!nameValidator.test(name)){
alert("Nome invalido")
return false}
}
That one !if
means the logical operator não
correct? My doubt is actually in condition
nameValidator.test(name)
That one namevalidator
, would not have to be called : validateName
,which is my function for validation of my user name?
So I have to have two onsubmits anyway correct? because having only the one of the name,?
– Carol M
and one more question,Function validateForm() { var ok = validateName(); if (ok) { ok = validateEmail(); } Return ok; Inside that if I’m validating both fields? or just email? Because I didn’t quite understand why the variable name is out of the if and the variable email is in!
– Carol M
@Carolm, in the role
validateName()
is already validating in the name as in your example, just need to create the function codevalidateEmail()
. I put so to answer exactly as you asked, but to simplify you can make the two validations within the functionvalidateForm
, I think it gets easier and clear– Ricardo Pontual
I put an example in the answer to the idea of validating everything in one Function
– Ricardo Pontual
now it’s even easier to understand! Thank you so much for helping Ricardo!
– Carol M