JavaScript Validation

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

  1. #1
    I using script

    PHP

    <?php
    
    include("db.php");
    
    $email = strip_tags(trim($_REQUEST['emailUsername']));
    
    $pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
    
    if (!eregi($pattern, $email))
    {
      echo "Ensure that your email address is valid.";
      die();
    }
    
    // Query database to check if the username is available
    $query = "select * from login_recruiters where username = '".$email."' ";
    $result = mysql_query($query);	
    $available = mysql_num_rows($result);
    
    if($available)
    {
    	echo $email." aleady registered with us.";
        die();
    }
    
    ?>
    PHP:
    how can I 'return false' when validation failed
     
    sanilts, Nov 22, 2010 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    ajax is asynchronous - which means it's going to be event driven. it does not return true/false. you should always return false instead to stop the event and based upon the outcome on the ajax, your xmlhttp.onreadystatechange callback can either output an error or post the form properly
     
    dimitar christoff, Nov 23, 2010 IP
  3. Jagbani

    Jagbani Greenhorn

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    <script type="text/javascript">
    <!--

    function validate_form ( )
    {
    valid = true;

    if ( document.contact_form.contact_name.value == "" )
    {
    alert ( "Please fill in the 'Your Name' box." );
    valid = false;
    }

    return valid;
    }

    //-->
    </script>
     
    Jagbani, Nov 25, 2010 IP