Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use onClick ONLY if email input field is valid?

Here's code:

<input id="subscribe-email" type="email" placeholder="[email protected]" />
            <button type="submit" id="subscribe-submit" onClick="javascript:omgemailgone();" />

How do I check and run JS function only if email is valid (validation by user-agent, no additional validations)?

UPDATE. New browsers can validate input=email by themselves, also there are pseudo classes :valid and :invalid. I need to run function only if browser 'knows' that email is valid.

like image 998
Demuri Celidze Avatar asked Mar 21 '13 16:03

Demuri Celidze


People also ask

How do you validate email input?

The <input type="email"> defines a field for an e-mail address. The input value is automatically validated to ensure it is a properly formatted e-mail address. To define an e-mail field that allows multiple e-mail addresses, add the "multiple" attribute. Tip: Always add the <label> tag for best accessibility practices!

Can I use onclick in input tag?

The onclick property of an Input object specifies an event handler function that is invoked when the user clicks on the input element. It is not invoked when the click( ) method is called for the element. Only form elements that are buttons invoke the onclick event handler.

What is email validation in JavaScript?

Email validation is one of the vital parts of authenticating an HTML form. Email is a subset or a string of ASCII characters, divided into two parts using @ symbol.


3 Answers

You can use the .checkValidity() method of the input element (in this case, the email input field). This will return a boolean indicating wether the input is valid or not. Here is a fiddle to play with:

http://jsfiddle.net/QP4Rc/4/

And the code:

<input id="subscribe-email" type="email" required="required" placeholder="[email protected]" />

<button type="submit" id="subscribe-submit" onClick="check()">
click me
</button>

function check()
{
  if(!document.getElementById("subscribe-email").checkValidity())
  {

    //do stuff here ie. show errors
    alert("input not valid!");

  }else
  {
      callMeIfValid();
  }
}

function callMeIfValid()
{
  //submit form or whatever
  alert("valid input");
}
like image 85
danii Avatar answered Oct 18 '22 13:10

danii


check Validate email address in JavaScript? for validation and then implement it into an if statement in your omgemailgone method (if valid continue, else do nothing)

edit:

function validateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\
".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA
-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}

from the link

like image 30
Rice_Crisp Avatar answered Oct 18 '22 15:10

Rice_Crisp


You can use Regular expressions to check that the email is valid on your omgemailgone() :

function omgemailgone (){
   var mail = $('#subscribe-email').val();

   //Example of regular expression
   if(mail.match(/YourRegexp/)
   {
      //Do stuff
   }
   else alert("Invalid e-mail");

}

(using jQuery here)

like image 26
Bigood Avatar answered Oct 18 '22 14:10

Bigood