0

I am trying to implement a sign-up form that asks users to input the desired login-id. The user inputs the desired login-id into a text box.

I would like to perform validation on this text box to see if the desired login-id is available without having to submit the form.

The validation should start happening say after the 5th character is input into the text box.

2 Answers 2

2

You should validate the complete username typed in. This will reduce your client server interaction too thus giving you performance too.

You can use jQuery's ajax method. Which will call your server side code and you can check from there by querying the database and can send back the date to client side.

HTML:

<input type="text" id="username" onblur="checkUsername(this);"/>

Javascript:

function checkUsername(element){
    $.ajax({
    url: server_url,
    data : element.value
    }).done(function(data) { //json object returned from server
        if(data.true){
           //allowed
        }else{
           alert("select another username");
        }
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Amit for the suggestion and the code snippet. Is there any way, this can be achieved using one of the drupal modules and an API call?
2

You can use the Client Side Validation module. This module does the validation and shows the error message before the submission of the form

Comments

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.