simon
simon

Reputation: 631

how I can know if a input e-mail has value in with jquery?

I try find a way of know if my input email has some values its in.

I tried this code:

var empty = true;


//get value from my input  type email
    $('input[type="email"]').each(function() {

 //check out if I have some values its in
    	if ($(this).val()!="") {
// if my input is empty then its should return this 

 //redefined my var with false
 empty = false;
 //return a console saying that my input is empty
    		return  console.log("empty yep");
    	}
    	
    });
       

what do I trying do?

I try create a login that when I put something over the input password my javascript detect if I have something in email if I do not have nothing then dont allow continue with the process login in my system without before put the email

some idea?

Upvotes: 0

Views: 164

Answers (2)

Ravi Ranjan
Ravi Ranjan

Reputation: 287

Add jquery library in head section of HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

then add an input tag in HTML code as below

<form name="form" action="" method="" onsubmit="validate()">
    <input type="email" name="email" id="email" value="" required>
</form>

then in jquery please paste the below code

if($('#email').val() == '') {
    alert('please provide the email').
    event.preventDefault();
    return false;
}

Note: you can even validate the email id.

Upvotes: 1

Maniraj Murugan
Maniraj Murugan

Reputation: 9084

Based on my understanding, You need to validate the email to enter the next password input box.

So I have made the password input disabled unless something is entered into email input box.

Using email.nextElementSibling.setAttribute('disabled',true); made the password field as disabled.

Then if you start entering into email then the password field will get enabled.

const email = document.querySelector('input[type="email"]');

checkEmailValidation = () => {
  if(email.value){
    email.nextElementSibling.removeAttribute("disabled");
  } else {
    email.nextElementSibling.setAttribute('disabled',true);
  }
}

email.addEventListener('input', checkEmailValidation);

checkEmailValidation();
<input type="email" placeholder="Type email here">
<input type="password" placeholder="Type password here">

Upvotes: 1

Related Questions