0

I am trying to validate a form using jQuery and PHP that will check to see if: a) the input is valid (valid email, etc) b) the username or email address already exists

Here is the JavaScript code on the register.php page:

<script language="javascript">
    $(document).ready(function()
{
    $("#wl-btn").click(function()
    {
    $("#msgbox").removeClass().addClass('messagebox').html('<div class="msg-info"> Validating form...</div>').fadeIn(1000);
    $.post("check_details.php",{ username:$('#wl-username').val(),password:$('#wl-password').val(),password2:$('#wl-password2').val(),email:$('#wl-email').val(),type:$('#wl-type').val(),rand:Math.random() } ,function(data)
    {
//////////// THIS IS THE CODE THAT I NEED TO ALTER TO CATER FOR CHECKING THE USERNAME AND EMAIL ADDRESS
        if(data=='userexists')
        {
        $("#msgbox").fadeTo(200,0.1,function()
        { 
            $(this).html('<div class="msg-error">That username has been taken! Please try another username.</div>').addClass('messageboxerror').fadeTo(900,1); 
        });
                    $("#wl-username").fadeTo(200,0.1,function()
                    {
                        $(this).addClass('input-error tiptip-bottom').fadeTo(900,1);
                    });
                    $("#wl-username-text").fadeTo(200,0.1,function()
                    {
                       $(this).text('Please choose a different username!').addClass('msg-form-error').fadeTo(900,1); 
                    });
////////////////////////////////////////
                } else if(data=='yes'){
                    $("#msgbox").fadeTo(200,0.1,function()
        { 
            $(this).html('<div class="msg-ok">Registration successful! Please check your email inbox for instructions on how to activate your account...</div>').addClass('messageboxok').fadeTo(900,1); 
        });
        } else {
        $("#msgbox").fadeTo(200,0.1,function()
        { 
            $(this).html('<div class="msg-error">Generic form error message.</div>').addClass('messageboxerror').fadeTo(900,1);
        });     
        }           
    });
    return false;
    });
});
</script>

As you can see, it is calling check_details.php and passing the POST values to that script. At the moment I have got this snippet of code to check if the user exists:

<?php

$query = mysql_query("SELECT * FROM users WHERE username='$username'");
$count = mysql_num_rows($query);
if ($count > 0) {
    echo "userexists";
}

?>

I need to find a way to validate the user input and check if the username and email address already exists in the database.

If the user input is in an incorrect format or the username/email address exists then I need the JavaScript code to add the appropriate error classes and messages (as seen in the first part of the JS code above).

hakre
  • 193,403
  • 52
  • 435
  • 836
Hammo
  • 182
  • 1
  • 9

1 Answers1

0

You can use json for server side validation(like for username/email id already exists or not). Its the best solution. You can check this thread also.

What is JSON and why would I use it?

Community
  • 1
  • 1
munjal
  • 1,384
  • 9
  • 15