0
function validateEmail(){

     var TCode = document.getElementById('email').value;


     if(TCode.length==0)
     {
         email_info.innerHTML="This field is required";
         return false;
     }
        email_info.innerHTML=" ";
        var atpos=TCode.indexOf("@");
        var dotpos=TCode.lastIndexOf(".");
        if (atpos<1 || dotpos<atpos+2 || dotpos+2>=TCode.length)
          {
             email_info.innerHTML="Not a valid e-mail address";
          return false;
          }
         email_info.innerHTML=" ";
            var xmlhttp;    

        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
             xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
                xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                 document.getElementById("email_info").innerHTML=xmlhttp.responseText;

            }
          }

        xmlhttp.open("GET","get_email.jsp?email="+TCode,true);
        xmlhttp.send();


        return true;  

}

here is my function which is called on onblur action on a text field which take user email id... what i want to do is to restrict the form from submitting, if email id already exist in database ...I am able to display text that email id already exist but i am not able to restrict user from submitting form even if it already exist what to do?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user2137186
  • 817
  • 6
  • 22
  • 39
  • You need to stop form from submitting, try this one: http://stackoverflow.com/questions/4227043/how-do-i-cancel-form-submission-in-submit-button-onclick-event – aledujke May 23 '13 at 13:17
  • In your condition , where checking if email exists - `return false;` should abort the form's submit. BTW , this question doesn't related to jQuery - so i'm removing this tag. – Ofir Baruch May 23 '13 at 13:17
  • @OfirBaruch I am sending it on a jsp page and there it checks whether it exists or not if it exists i display text ...and return false not working here: if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("email_info").innerHTML=xmlhttp.responseText; } – user2137186 May 23 '13 at 13:20
  • 3
    out of interest... given that the question originally had a `jquery` tag, I assume you know what jQuery is and possibly even use it in your code. If that's true, why on earth have you got all that hand-written ajax handling code when you could simply write a single line of jQuery code? – Spudley May 23 '13 at 13:23

1 Answers1

0

First you set a variable to false

window.isOkayToSubmit = false;

then you set that variable to true if email is okay after you check it:

if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
  document.getElementById("email_info").innerHTML=xmlhttp.responseText;
  window.isOkayToSubmit = true;
}

then on form submit:

<form onsubmit="return isValidForm()" />

you just return that val:

function isValidForm(){
  return window.isOkayToSubmit;
}

additionaly, if I understand your code, you set it to false whenever the email was wrong, in case somenoe provides good email, then changes it to bad one:

if (atpos<1 || dotpos<atpos+2 || dotpos+2>=TCode.length)
{
  email_info.innerHTML="Not a valid e-mail address";
  window.isOkayToSubmit = false;
  return false;
}
aledujke
  • 1,125
  • 7
  • 15
  • i tried that but i cant set my value there ...i don't know why...i tried to set my value in that if but it returns false the initial value that was set:S – user2137186 May 23 '13 at 13:28
  • Are you saying that isOkayToSubmit never gets set to true? Are you sure that code ever executes? – aledujke May 23 '13 at 13:31