Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check username availability

I have a form to user login:

<%= form_tag(@action, :method => "post", :name => 'signup' ,:onSubmit => 'return validate();') do %>    
  <%= label_tag(:user, "Username:") %>
  <%= text_field_tag(:user) %>

I want to check if there is the username in the database immediately after :user-field lost focus. I can override this event on the form with javascript, but I can not send Ruby-AJAX request from javascipt code.

Is there any way to check username without adding additional controls (buttons, links) on the form?

like image 203
ceth Avatar asked Apr 28 '11 08:04

ceth


People also ask

How do I check availability of usernames?

With Namechk, you can check the availability of a username or domain name within seconds. There are 351 million registered domain names and counting. Every day, thousands more are registered.

How do I check if a username is available on social media?

To check the availability of your username or handle, perform the following steps. Open the Social Media Name Checker. Enter the unique ideas, brand names, or usernames for your social media and go for a search. The tool searches your provided username or handle on all the top social media platforms.

How do you check if Instagram username is taken?

The easiest and quickest way to do this is to use BrandSnag's Instagram Name Checker. This tool will let you test out multiple handle variations until you find the one that's free. You can then set up your Instagram account faster as you'll already be armed with an available username.

How do I check if a YouTube username is available?

The easiest way to check if a YouTube username is still available for use is by simply trying to make a Google account with your desired username as your YouTube's official email address. Google will inform you with a “That username is taken.


1 Answers

You can use some JavaScript (this one written with jQuery) for AJAX cheking:

$(function() {
    $('[data-validate]').blur(function() {
        $this = $(this);
        $.get($this.data('validate'), {
            user: $this.val()
        }).success(function() {
            $this.removeClass('field_with_errors');
        }).error(function() {
            $this.addClass('field_with_errors');
        });
    });
});

This JavaScript will look for any fields with attribute data-validate. Then it assings onBlur event handler (focus lost in JavaScript world). On blur handler will send AJAX request to the URL specified in data-validate attribute and pass parameter user with input value.

Next modify your view to add attribute data-validate with validation URL:

<%= text_field_tag(:user, :'data-validate' => '/users/checkname') %>

Next add route:

resources :users do
  collection do
    get 'checkname'
  end
end

And last step create your validation:

class UsersController < ApplicationController
  def checkname
    if User.where('user = ?', params[:user]).count == 0
      render :nothing => true, :status => 200
    else
      render :nothing => true, :status => 409
    end
    return
  end

  #... other controller stuff
end
like image 82
Viacheslav Molokov Avatar answered Sep 22 '22 13:09

Viacheslav Molokov