0

Hello everyone ! Here a question :D I have this code

function validateName(){
    if ( name.val().length <= 4 ){
        name.removeClass('valid');
        nameInfo.removeClass('valid');
        name.addClass('error');  
        nameInfo.addClass('error');
        nameInfo.text (" You need to have more than 4 letters ");
        state = false;
    }else{
        var uname = name.val();
        $.post('validateuser.php' , {names:uname} , function(data){
            if (data != 1){
                name.removeClass('valid');
                nameInfo.removeClass('valid');
                name.addClass('error');
                nameInfo.addClass('error');
                nameInfo.text("This name is already registered!");
                state = false;
            }else{
                name.removeClass('error');
                nameInfo.removeClass('error');
                name.addClass('valid');
                nameInfo.addClass('valid');
                nameInfo.text('Valid name');
                state = true;
                }
            });


        }

    return state;
}

and this one

$("#send").click(function(){
    if( validateName() == true ){
        alert('works');
});

And it actually won't work .. Any suggestions?

2
Contributors
7
Replies
16
Views
4 Years
Discussion Span
Last Post by MWEB
0

Is there more code? When looking at your function, the first thing that comes to mind is that you are referencing name.val(). What does this object represent? An input element? Where is the assignmnet of this element to a variable?

0

Yes , the name is a variable that holds $('#name') ( name is the id from the input ) Should i post the HTML code too?

0

You could...sometimes people post too much code and it becomes difficult to troubleshoot.

If you have this site online, a link would work since we can look at the source code. Another option is to upload your code to jsfiddle.net and save the link.

0

HTML :

<form id='customForm'>
    <div>
        <label for='name'>Name </label>
        <input type='text' id='name' name='name'  />
        <span id='nameInfo'>Please type a valid username</span>
    </div>
    <div>
<input id='send' name='send' type='button' value='Send' />
    </div>
</form>

JS :

$(document).ready(function(){

var form = $('#customForm');
var name = $('#name');
var nameInfo = $('#nameInfo');
var state = false;

name.keyup(validateName);

function validateName(){
    if ( name.val().length <= 4 ){
        name.removeClass('valid');  // stergi clasa daca exista
        nameInfo.removeClass('valid');
        name.addClass('error');  // adaugi clasa error
        nameInfo.addClass('error');
        nameInfo.text (" You need to have more than 4 letters ");
        state = false;
    }else{
        var uname = name.val();
        $.post('validateuser.php' , {names:uname} , function(data){
            if (data != 1){
                name.removeClass('valid');
                nameInfo.removeClass('valid');
                name.addClass('error');
                nameInfo.addClass('error');
                nameInfo.text("This name is already registered!");
                state = false;
            }else{
                name.removeClass('error');
                nameInfo.removeClass('error');
                name.addClass('valid');
                nameInfo.addClass('valid');
                nameInfo.text('Valid name');
                state = true;
                }
            });


        }

    return state;
}

    $("#send").click(function(){
    if( validateName() == true ){
        alert('works');
    }else{
    alert(' does not work ');
    }
    });
});

And this Link on jsfiddle

2

Ok, great job....

When I run this code and check using a browser's tool, its evident there's a syntax error which is preventing your script from running...a closer look using jsfiddle, shows that you did not close the if statement in this section...

if( validateName() == true ){alert('works');}   

You were missing the closing curly brace.

When I rerun in my browser watching with dev tools, after I type in the 5 character, the browser tries to access "validateuser.php" ( i see it in the javascript console) but fails for me... which is expected.

Edited by JorgeM

0

Yes , now i have another problem .. i have more functions and when i try to verify them all it does not work (I mean that .. all the functions are true and it gives me the check on your email error ), look the code:

PS: The input box is green .. what means that it is a valid e-mail , after the SQL and Pattern verification

$("#send").click(function(){
    if(validateName() == true ){
        if(validateEmail() == true ){
            if(validatePass1() == true ){
                if(validatePass2() == true ){
                    alert("worked")
                }else{
                    alert('Check on your confirmed password');
                } 
            }else{
                alert('Check on your password');
            } 
        }else{
            alert('Check on your email');
        } 
    }else{
        alert('Check on your username');
    } 
});

the validateEmail function is :

function validateEmail(){
    var uemail = email.val();
    var filter = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);
    if(filter.test(uemail)){
        $.post('validatemail.php' , {emails:uemail} , function(data){
            if (data != 1){
                email.removeClass('valid');
                emailInfo.removeClass('valid');
                email.addClass('error');
                emailInfo.addClass('error');
                emailInfo.text("This email is already in use");
                state = false;
            }else{
                email.removeClass('error');
                emailInfo.removeClass('error');
                email.addClass('valid');
                emailInfo.addClass('valid');
                emailInfo.text('Valid email');
                state = true;
                }
        });
    }else{
        email.removeClass('valid');
        emailInfo.removeClass('valid');
        email.addClass('error');
        emailInfo.addClass('error');
        emailInfo.text("Please type a valid email !");
        state = false;
    }
}

Edited by MWEB

0

I've worked on this and found the problem , i've missed a return state; , that's it :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.