First, I’m a total newbie at JS. I’m using this pratically self-made script to check if email and display name exist on database, using AJAX and jQuery. Each of the text inputs have a button to manually check, else it checks onBlur with the accordingly function.
The username one works fine but the email only works if I manually click to check. I’ve been trying to fix it but no luck at all.
[CODE]<script type=”text/javascript”>
$(document).ready(function() {
//the min chars for username
var display_min_chars = 3;
var display_max_chars = 20;
var email_min_chars = 5;
var email_max_chars = 120;
//result texts
var characters_display_error = ‘Length of display name: 3-20 chars.’;
var checking_display_html = ‘Checking…’;
var characters_email_error = ‘Length of email: 5-120 chars.’;
var checking_email_html = ‘Checking…’;
$(‘#check_email_availability’).click(function(){
if($(‘#email’).val().length < email_min_chars || $(‘#email’).val().length > email_max_chars){
$(‘#email_availability_result’).html(characters_email_error);
document.registerForm.submit_reg.disabled = true;
}else{
$(‘#email_availability_result’).html(checking_email_html);
check_email_availability();
}
});
$(‘#check_username_availability’).click(function(){
if($(‘#username’).val().length < display_min_chars || $(‘#username’).val().length > display_max_chars){
$(‘#username_availability_result’).html(characters_display_error);
document.registerForm.submit_reg.disabled = true;
}else{
$(‘#username_availability_result’).html(checking_display_html);
check_display_availability();
}
});
});
//function to check username availability
function check_display_availability(){
var min_chars = 3;
var max_chars = 20;
//result texts
var characters_error = ‘Length of display name: 3-20 chars.’;
var checking_html = ‘Checking…’;
if($(‘#username’).val().length < min_chars || $(‘#username’).val().length > max_chars){
$(‘#username_availability_result’).html(characters_error);
document.registerForm.submit_reg.disabled = true;
return;
}else{
//else show the cheking_text and run the function to check
$(‘#username_availability_result’).html(checking_html);
//get the username
var username = $(‘#username’).val();
//use ajax to run the check
$.post(“http://www.tf2warehouse.com/user/check_username”, { username: username },
function(result){
if(result == “available”){
$(‘#username_availability_result’).html(‘Display name available!’);
document.registerForm.submit_reg.disabled = false;
}else{
if(result == “invalid”){
$(‘#username_availability_result’).html(‘Display name is invalid.’);
document.registerForm.submit_reg.disabled = true;
}
if(result == “notavailable”){
$(‘#username_availability_result’).html(‘Display name is not available.’);
document.registerForm.submit_reg.disabled = true;
}
if(result == “empty”){
$(‘#username_availability_result’).html(‘Display name is empty.’);
document.registerForm.submit_reg.disabled = true;
}
}
});
}
}
function check_email_availability(){
$(‘#email_availability_result’).html(checking_email_html);
var email_min_chars = 5;
var email_max_chars = 120;
//result texts
var email_characters_error = ‘Length of email: 5-120 chars.’;
var email_checking_html = ‘Checking…’;
if($(‘#email’).val().length < email_min_chars || $(‘#email’).val().length > email_max_chars){
$(‘#email_availability_result’).html(email_characters_error);
document.registerForm.submit_reg.disabled = true;
return;
}else{
//else show the cheking_text and run the function to check
$(‘#email_availability_result’).html(email_checking_html);
//get the username
var email = $(‘#email’).val();
//use ajax to run the check
$.post(“http://www.tf2warehouse.com/user/check_email”, { email: email },
function(result2){
if(result2 == 0){
$(‘#email_availability_result’).html(‘Email available!’);
document.registerForm.submit_reg.disabled = false;
}else{
if(result2 == “invalid”){
$(‘#email_availability_result’).html(‘Email is invalid.’);
document.registerForm.submit_reg.disabled = true;
}
if(result2 == 1){
$(‘#email_availability_result’).html(‘Email is already in use!’);
document.registerForm.submit_reg.disabled = true;
}
if(result2 == “empty”){
$(‘#email_availability_result’).html(‘Email is empty.’);
document.registerForm.submit_reg.disabled = true;
}
}
});
}
}
</script>