This jQuery form validation is not working. When I click on the submit button the Submit2
function isn't doing any validation. I'm sure I used the correct syntax and correct order of the loading of jQuery.
<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;
}
}
});
==
or triple equals ===
to check for a condition in if
statements. a single =
is a assignment</div>
in the endautocomplete="off"
on the form instead of doing it on every form elementform#onSubmit
instead of button#onClick
to let constraint validation kick in. But to do so you should change the button to type="submit"
from type="button"
, wish you should do anyway=""
on attributes just by having required
is enough. // short for document ready // https://api.jquery.com/ready/ jQuery($ => { // Here we use form#onSubmit instead of button click to // let the constraint validation kick in $('#teacher-form').on('submit', evt => { // Disable default behaviour (stop from submitting) evt.preventDefault() // This should always be true... since this only happens when the form is valid let valid = evt.target.checkValidity() ? 'valid' : 'invalid' alert('The form is ' + valid) }) })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="signup_teacher"> <form class="form_signin" id="teacher-form" method="post" action="" autocomplete="off"> <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 placeholder="Email" id="email2"> </div> </div> <div class="prompt_teacher_id"> <div class="field" data-fieldname="teacher_id"> <input type="number" required maxlength="7" placeholder="Id" id="idno2"> </div> </div> <div class="prompt_password"> <div class="field" data-fieldname="password"> <input type="password" required placeholder="Password" id="pswrd2"> </div> </div> <div class="btn-group sign-up-box"> <button type="submit" class="btn btn-block btn-lg btn-primary" id="submit2">Submit</button> </div> </form> </div>
the onclick
needs the function to be available in the global window scope. However you have placed the function inside document.ready
so it is not accessible in global scope. Using your browser console you should see error thrown that Submit2
is undefined (or not a function).
Since you are using jQuery it would be simpler to use a jQuery event handler and remove onclick
from the html
$(document).ready(function() {
function Submit2() {
// your existing function code
}
$('#teacher-form').on('submit', Submit2);
});
There are two things you have to change. DEMO
submit2()
inside the document.ready
which won't be visible to window scope, hence you can remove onclick submit function from the html and call the function onclick of submit in jQuery using the button id"=="
double equals to check for a conditionHTML
<button type="button" class="btn btn-block btn-lg btn-primary " id="submit2" >Submit</button>
jQuery
$(document).ready(function() {
$('#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;
}
if($("#email2").val()=="")
{
$("#email2").focus();
return false;
}
else if($("#idno2").val()=="")
{
$("#idno2").focus();
return false;
}
else if($("#pswrd2").val()=="")
{
$("#pswrd2").focus();
return false;
}
});
});
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>
Using <... onclick="someFns"> to register a handle, the someFns must be accessible in the global scope. However, in your code, 'Submit2' function is inside a anonymous function, and JS using function scope, so the 'Submit2' function can only be reached inside the anonymous function.
If you want to use the <... onclick...> way to register handler, make 'Submit2' a global function.
Another way you can register the function :
$(function(){
function Submit2 () {
...
}
$('#submit2').click(Submit2);
})
Another thing, what do you expect the return value doing?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.