Tushar Gupta Tushar Gupta - 7 months ago 11
Javascript Question

I have used the correct syntax of jquery but still isn't working?

**Please help in this a s jquery of form validation is not working. when i click on the submit button the jquery is not doing the validation. I am sure i have used the correct syntax and correct order of the loading of jquery. Please reply **

<div id="signup_teacher">
<form class="form_signin" id="teacher-form" method="post" action="">
<h2 class="form_signin_heading signin_header">Signup.</h2>
<div class="field" data-fieldname="name">
<div class="prompt_name">
<input type="text" required="" autocomplete="off" maxlength="20" placeholder="Full Name" id="fullname2">
</div>
</div>

<div class="prompt_email">
<div class="field" data-fieldname="email">
<input type="email" required="" autocomplete="off" placeholder="Email" id="email2">
</div>
</div>

<div class="prompt_teacher_id">
<div class="field" data-fieldname="teacher_id">
<input type="number" required="" autocomplete="off" maxlength="7" placeholder="Id" id="idno2">
</div>
</div>

<div class="prompt_password">
<div class="field" data-fieldname="password">
<input type="password" required="" autocomplete="off" placeholder="Password" id="pswrd2">
</div>
</div>

<div class="btn-group sign-up-box">
<button type="button" class="btn btn-block btn-lg btn-primary " id="submit2" onclick="Submit2">Submit</button>
</div>

</form>
</div>

</div>






$(document).ready(function()
{

function Submit2()
{
var fullname2=$("#fullname2").val();
var email2=$("#email2").val();
var idno2=$("#idno2").val();
var pswrd2=$("#pswrd2").val();


if($("#fullname2").val()="")
{
$("#fullname2").focus();
return false;
}
if($("#email2").val()="")
{
$("#email2").focus();
return false;
}
else if($("#idno2").val()="")
{
$("#idno2").focus();
return false;
}
else if($("#pswrd2").val()="")
{
$("#pswrd2").focus();
return false;
}

}


});



Answer

You are mixing JavaScript and jquery. use .click() event handler on button and secondly change '=' to '==' in if statement.

    $('#submit2').click(function() {
    	var fullname2=$("#fullname2").val();
        var email2=$("#email2").val();
        var idno2=$("#idno2").val();
        var pswrd2=$("#pswrd2").val();


        if($("#fullname2").val()=="")
        {
              $("#fullname2").focus();
                return false;
        }
        else if($("#email2").val()=="")
        {
            $("#email2").focus();
            return false;
        }
        else if($("#idno2").val()=="")
        {
            $("#idno2").focus();
            return false;
        }
        else if($("#pswrd2").val()=="")
        {
              $("#pswrd2").focus();
            return false;
        }
    })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="signup_teacher">
<form class="form_signin" id="teacher-form" method="post" action="">
<h2 class="form_signin_heading signin_header">Signup.</h2>
            <div class="field" data-fieldname="name">
                <div class="prompt_name">
                    <input type="text" required="" autocomplete="off" maxlength="20" placeholder="Full Name" id="fullname2">
                </div>
            </div>

            <div class="prompt_email">
                <div class="field" data-fieldname="email">
                    <input type="email" required="" autocomplete="off"  placeholder="Email" id="email2">
                </div>
            </div>

            <div class="prompt_teacher_id">
                <div class="field" data-fieldname="teacher_id">
                    <input type="number" required="" autocomplete="off" maxlength="7" placeholder="Id" id="idno2">
                </div>
            </div>

            <div class="prompt_password">
                <div class="field" data-fieldname="password">
                    <input type="password" required="" autocomplete="off" placeholder="Password" id="pswrd2">
                </div>
            </div>

            <div class="btn-group sign-up-box">
                <button type="button" class="btn btn-block btn-lg btn-primary " id="submit2">Submit</button>
            </div>

    </form>
    </div>