Validation for input box, use Jquery focusin function doesn't work

170 views Asked by At

I am making a validation form for people creating new accounts on the website. when user enter second input box, they will get error message if the first input box (username) is empty or there is a same username already in database. How to show the error message when they start entering second input box? i used focusin but it didn't work. Could you help me??

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("input[name='last_name']")    .focusin(function(){
    if($("input[name='first_name']")=="")
    {
        $("#firstname_error").html("Please enter your firstname")
        }

    })
})
</script>
</head>
<body>
<input type="text" name="first_name" placeholder="First Name" onFocus="this.placeholder=''" onBlur="this.placeholder='First Name'" />
                    <div id="firstname_error" class="error_message hide_max_desktop"></div> <!-- end firstname_error -->
                    <input type="text" name="last_name" placeholder="Last Name" onFocus="this.placeholder=''" onBlur="this.placeholder='Last Name'" />
                    <div id="lastname_error" class="error_message hide_max_desktop"></div>

</body>
</html>
1

There are 1 answers

0
epascarello On BEST ANSWER

Because

if($("input[name='first_name']")=="")

is not checking the value, you are comparing a jQuery object to a blank string.

$("input[name='first_name']").val()