0

I want to check the email validity in Javascript and then show alert box if its invalid and if valid then show the div2. But its not working

Here is javascript:

function _(x){
    return document.getElementById(x);
}

function Phase1()
{       
    myemail = _("email").value;     
    var reg = /^(([^<>()[\]\\.,;:\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,}))$/;

    if (reg.test(designeremail) == false) 
    {
        alert('Invalid Email Address');            
    }
    else
    {
    _("phase1").style.display = "none";
    _("phase2").style.display = "block";

    }
}

Here is my html:

<form id="myform" onsubmit="return false" >    
    <div id="phase1">                 
            <label>Email</label><div><input type="text" id="email" name="email"  required></div>                
            <div><button onclick="Phase1()" class="btn"><strong>Next</strong></button></div>
    </div> 
    <div id="phase2">                 
            <label>Name</label><div><input type="text" id="mname" name="myname"  required></div>                
            <div><button onclick="Phase2()" class="btn"><strong>Submit</strong></button></div>
    </div> 
</form>

but even with wronng email it proceeds and shows the phase2

pravindot17
  • 1,199
  • 1
  • 15
  • 32
  • in if condition you are using wrong variable, please use `if (reg.test(myemail) == false)` instead `if (reg.test(designeremail) == false)` – pravindot17 May 25 '18 at 09:48
  • had tried with both php and javascript but tht doesnt work either ..plz see https://stackoverflow.com/questions/50523718/validate-email-and-show-div?noredirect=1#comment88059373_50523718 – user4697402 May 25 '18 at 09:53
  • please see this pattern, https://stackoverflow.com/a/940609/6582942 – pravindot17 May 25 '18 at 09:54

1 Answers1

0

I think this is js and not php. Btw, it's because probably you're testing on the wrong variable:

if (reg.test(designeremail) == false) you're testing designeremail, which is different from email from the form which probably you wanted test against to.

Federico Ponzi
  • 2,682
  • 4
  • 34
  • 60