Please help me for JavaScript Validation in from

Discussion in 'JavaScript' started by sanilts, Nov 23, 2010.

  1. #1
    i have a web form with user registration, what i want to do is to find the entered username is exist in the database, if exit don't submit the form.

    I used the code below, the problem is even if the username is exist the form will submit the value, if alter i can see the error showing but when i cancel the alter message box it will submit the form. please help me.........

    function emailCheck()
    {
    
    str=document.forms['recruiter'].username.value;
    
    
    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("username").innerHTML=xmlhttp.responseText;
    	}
      }
    
    xmlhttp.open("GET","lib/validate_email_user.php?username="+str,true);
    xmlhttp.send();
    }
    Code (markup):

     
    sanilts, Nov 23, 2010 IP
  2. MattCA

    MattCA Peon

    Messages:
    14
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you want to stop submit process and stay on the site, you need to return false.
     
    MattCA, Nov 24, 2010 IP
  3. s.jns

    s.jns Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    Javascript is user side, you can't check the existing username in database with JS but This could be solve with server side code, what server side code You already use?
     
    s.jns, Nov 24, 2010 IP
  4. sanilts

    sanilts Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    with the help of php and ajax i can display the validation failure that is the username is exist in the database, the problem is even after displaying the error when is i press the submit button it will submit data in to database....:)
     
    sanilts, Nov 25, 2010 IP
  5. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #5
    You need to add it to the 'onblur' handler of the textbox for the username.... not the 'submit' event of the form...
     
    camjohnson95, Nov 28, 2010 IP