I'm trying to figure out how I can turn this:
$('#username').blur(function(){
$.post('register/isUsernameAvailable',
{"username":$('#username').val()},
function(data){
if(data.username == "found"){
alert('username already in use');
}
}, 'json');
});
into something close to this:
rules: {
username: {
minlength: 6,
maxlength: 12,
remote: {
url: 'register/isUsernameAvailable',
type: 'post',
data: {
'username': $('#username').val()
}
}
}
However I'm having a hard time finishing it off. What I want is instead of the alert to have it display the error message but I can set the message inside the actual jquery validation messages.
http://docs.jquery.com/Plugins/Validation/Methods/remote#options
UPDATE:
For some reason its not doing it as a POST its doing it as a GET request and not sure why. Here's the updated code:
rules: {
username: {
minlength: 6,
maxlength: 12,
remote: {
url: 'register/isUsernameAvailable',
dataType: 'post',
data: {
'username': $('#username').val()
},
success: function(data) {
if (data.username == 'found')
{
message: {
username: 'The username is already in use!'
}
}
}
}
},
UPDATE 2:
Now I'm getting somewhere I'm back to getting the POST request. I'm getting two more problems. One of which is the fact that for another POST request to be done the user has to refresh the form. And the last problem is that if the returned username is found it DOES NOT show the error message.
rules: {
username: {
minlength: 6,
maxlength: 12,
remote: {
type: 'post',
url: 'register/isUsernameAvailable',
data: {
'username': $('#username').val()
},
dataType: 'json',
success: function(data) {
if (data.username == 'found')
{
message: {
username: 'The username is already in use!'
}
}
}
}
},
UPDATE:
public function isUsernameAvailable()
{
if ($this->usersmodel->isUsernameAvailable($this->input->post('username')))
{
return false;
}
else
{
return true;
}
}
UPDATE 4:
Controller:
public function isUsernameAvailable()
{
if ($this->usersmodel->isUsernameAvailable($this->input->post('username')))
{
return false;
}
else
{
return true;
}
}
public function isEmailAvailable()
{
if ($this->usersmodel->isEmailAvailable($this->input->post('emailAddress')))
{
return false;
}
else
{
return true;
}
}
MODEL:
/**
* Check if username available for registering
*
* @param string
* @return bool
*/
function isUsernameAvailable($username)
{
$this->db->select('username');
$this->db->where('LOWER(username)=', strtolower($username));
$query = $this->db->get($this->usersTable);
if ($query->num_rows() == 0)
{
return true;
}
else
{
return false;
}
}
/**
* Check if email available for registering
*
* @param string
* @return bool
*/
function isEmailAvailable($email)
{
$this->db->select('email');
$this->db->where('LOWER(email)=', strtolower($email));
$query = $this->db->get($this->usersTable);
if($query->num_rows() == 0)
{
return true;
}
else
{
return false;
}
}
true(boolean), then validation for the rule succeeds. If a string result is returned, that is used as the error message.