Click here to Skip to main content
15,917,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an email textbox (required field) and a button. I want that whenever a user puts his mail id and clicks the button a JavaScript popup should appear.

My problem is when I click the button, that popup is appearing before the required-field msg.

I want JavaScript code written in a way that if textbox is empty then it will show a "please enter email id" validation message and if user gives his email id then that popup will appear; otherwise not.

pls help me out


this is my HTML


XML
<div class="input-append">
              <input type="email" id="email" name="email" placeholder="Your Email"/>
              <button class="subscribe-button btn" type="submit" onclick="myFunction()">SUBSCRIBE</button>
            </div>


and this is my javascript

XML
<script type="text/javascript">
function myFunction() {
    alert("You have done it");
}
</script>
Posted

XML
Hi,Try This :-)

<html>
    <body>
       <form id="MainForm">
        <input type="email" id="inputEmail" required/>
        <input type="submit" id="cmd" value="Submit"/>
        <script type="text/javascript">
            var cmd=document.getElementById("cmd");
                cmd.onclick=function(e){
                    var form=document.getElementById("MainForm");
                    if (form.checkValidity()){
                        showStatus("You have done it");
                    }
                }
                function showStatus(e){
                    alert(e);
                }
        </script>
        </form>
    </body>
</html>
 
Share this answer
 
Comments
Ali64b 22-Sep-14 9:12am    
And customize your validation


>>document.getElementById("inputEmail").validity.valid
<<true or false
>>var q = document.getElementById("inputEmail")
<<undefined
>>q.validationMessage
<<"Please fill out this field."
>>var q = document.getElementById("inputEmail")
<<undefined
>>q.validationMessage
<<"Please include an '@' in the email address. 'dd' is missing an '@'."
fiddle to check the email required field[^]

Please check out this fiddle, I hope this solves your problem.
If not reply back.. :)
Thanks
Happy Coding :)
 
Share this answer
 
Just call the below method
C#
function checkEmail() {

    var email = document.getElementById('txtEmail');
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if (!filter.test(email.value)) {
    alert('Please provide a valid email address');
    email.focus;
    return false;
 }


Read more here
http://www.w3resource.com/javascript/form/email-validation.php[^]
 
Share this answer
 
 
Share this answer
 
You need to write something like this:
C#
function myFunction() {
       var enteredEmail = document.getElementById('email').value;
       if (enteredEmail == "") {
           alert("Please enter email id");
       }
       else {
           alert("You have done it");
       }
   }


There are a lot of tutorials available just google for it.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900