0

EDIT: document.getElementById('submit').disabled='disabled'; does the trick if I put it instead of return false; but for some reasons, it also disables the button when the username is NOT taken.


Below is basic script to check a username availability.

How can I prevent the form from being submitted if a username is already taken?

Ideally i would like to have a JS alert box pop up.

I've tried to add this to the JS but didn't work:

document.getElementById('submit').disabled

Also I've tried to add onclick="return validate();" to the form itself, but no luck either: the form still can still get submitted.

HTML

<form id="edit" action="edit.php" method="post">
   <fieldset>       
     <label>Username</label><input type="text" class="input" name="username"      
     id="username"/><span id="status"></span> 
    <button type="submit" id="submit" value="add">Save</button>
   </fielset>
</form>

Script

<script type="text/javascript">
$(document).ready(function() {
    $("#username").change(function() {
        var username = $("#username").val();
        var msgbox = $("#status");

        if (username.length >= 4) {
            $("#status").html('<img src="images/gif/ajax-loading.gif">');

            $.ajax({
                type: "POST",
                url: "ajax_check.php",
                data: "username=" + username,
                success: function(msg) {

                    if (msg == 'OK') {

                         msgbox.html('<img src="/images/yes.png">');
                         return true;

                    } else {
                        msgbox.html(msg);            
                        return false;

                    }
                }
            });
        } else {

            $("#status").html('<img src="/images/no.png">too long!');
            return false;
        }
        return false;
    });
});

edit.php and ajax_check.php only contain some SQL queries.

Also, some users have JavaScript disabled on their browser, how could I get around this?

6
  • I think onclick="return validate();" should do the trick. Where you placed that? it should be in the button element. There is no way to force JS execution, actually the page can be accessed with some download utilities (wget for instance) that don't have any idea what JS is. :) Commented Sep 15, 2013 at 4:17
  • @AntoanMilkov I just did as you said and yet, the submit button is still clickable, therefore the form gets submitted. I really need to make sure that no customers can have the same usermame. Commented Sep 15, 2013 at 4:20
  • Shouldn't it be on the form onsubmit event? Like <form onsubmit="return validate();">. Commented Sep 15, 2013 at 4:22
  • 1
    Change it to:document.getElementById('submit').disabled='disabled' as see what happens Commented Sep 15, 2013 at 4:22
  • Can you paste the code for the function validate ? Commented Sep 15, 2013 at 4:22

2 Answers 2

1

This should do the trick. Re-enable the button on success and disable it on failure.

   if (msg == 'OK') {                       
            msgbox.html('<img src="/images/yes.png">');
            document.getElementById('submit').disabled = false;  //enable submit

      } else {                    
            msgbox.html(msg);            
            document.getElementById('submit').disabled = 'disabled';  //disable submit
      }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks ! It works now !! Also what should I do for users with Javascript disabled. Is there any way to get around this and also use some PHP to disable the button ?
You should not rely only on client side validation. Use proper server side validation too. Client side validation is just for better user experience. Anyone can bypass these validations. You can check if the user is registered or not after form submission and display proper message accordingly.
1

You can add to the attribute to the form element

onsubmit="return validate();"

If JavaScript is disabled then there is nothing you can do about it. This is why it is imperative that you apply data validation/santiation on the server-side.

2 Comments

thanks but the form can still be submitted. Just to be clear, onsubmit="return validate();" should be places within the submit button, right ?
@BenRifff, no it can be placed in either location, just a different syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.