In Ajax I just want to show the validation of the input that the user interacts with. Not the show all validations. In my script if you write anything to any input you will get all inputs validations in the same time. Its show all validations.
I mean if you write 3 in fullname input then validations says: "Fullname must be alphanumerical" but in the same time also show username, email and password validations too. It shows all the validations. I try to prevent this thing but can’t catch the logical vision. How can I achieve this?
<script type="text/javascript">
$(document).ready(function() {
$("#register-form").keyup(function(event) {
event.preventDefault();
var fullname = $("#fullname").val();
var username = $("#username").val();
var email = $("#email").val();
var password = $("#password").val();
$.ajax({
type: "POST",
url: "registercontrol.php",
data: {
fullname: fullname,
username: username,
email: email,
password: password
},
dataType: "json",
success: function(result) {
$('.hidden').show();
$('#result').html(result);
$("#vfullname").html("");
$("#fullname").removeClass();
$("#fullname").addClass("form-control form-control-user border-success");
$("#vusername").html("");
$("#username").removeClass();
$("#username").addClass("form-control form-control-user border-success");
$("#vemail").html("");
$("#email").removeClass();
$("#email").addClass("form-control form-control-user border-success");
$("#vpassword").html("");
$("#password").removeClass();
$("#password").addClass("form-control form-control-user border-success");
if (result.fni) {
$("#vfullname").html(result['fni']);
$("#vfullname").removeClass();
$("#vfullname").addClass("text-info");
$("#fullname").removeClass();
$("#fullname").addClass("form-control form-control-user border-info");
}
if (result.fnw) {
$("#vfullname").html(result['fnw']);
$("#vfullname").removeClass();
$("#vfullname").addClass("text-warning");
$("#fullname").removeClass();
$("#fullname").addClass("form-control form-control-user border-warning");
}
if (result.uni) {
$("#vusername").html(result['uni']);
$("#vusername").removeClass();
$("#vusername").addClass("text-info");
$("#username").removeClass();
$("#username").addClass("form-control form-control-user border-info");
}
if (result.unw) {
$("#vusername").html(result['unw']);
$("#vusername").removeClass();
$("#vusername").addClass("text-warning");
$("#username").removeClass();
$("#username").addClass("form-control form-control-user border-warning");
}
if (result.une) {
$("#vusername").html(result['une']);
$("#vusername").removeClass();
$("#vusername").addClass("text-danger");
$("#username").removeClass();
$("#username").addClass("form-control form-control-user border-danger");
}
if (result.mi) {
$("#vemail").html(result['mi']);
$("#vemail").removeClass();
$("#vemail").addClass("text-info");
$("#email").removeClass();
$("#email").addClass("form-control form-control-user border-info");
}
if (result.mw) {
$("#vemail").html(result['mw']);
$("#vemail").removeClass();
$("#vemail").addClass("text-warning");
$("#email").removeClass();
$("#email").addClass("form-control form-control-user border-warning");
}
if (result.me) {
$("#vemail").html(result['me']);
$("#vemail").removeClass();
$("#vemail").addClass("text-danger");
$("#email").removeClass();
$("#email").addClass("form-control form-control-user border-danger");
}
if (result.pwi) {
$("#vpassword").html(result['pwi']);
$("#vpassword").removeClass();
$("#vpassword").addClass("text-info");
$("#password").removeClass();
$("#password").addClass("form-control form-control-user border-info");
}
if (result.pww) {
$("#vpassword").html(result['pww']);
$("#vpassword").removeClass();
$("#vpassword").addClass("text-warning");
$("#password").removeClass();
$("#password").addClass("form-control form-control-user border-warning");
}
}
});
});
});
</script>
Thank you
2
Answers
With help of AlwaysHelping.
I made some the validations on Javascript side that do not require database connection and Php and I processed each one separately in the
.keyup
function, so only the input that was actually interacting was giving the validations error and resolved other Ajax-requiring verification in a single Ajax call.Here is much simpler answer for you by using
this
and use of.attr
instead of doing multiplekeyup
functions i am only doing one and usingif
conditions to match.attr
of input i am typing in.I have also assigned unique class
.validate_input
to all inputs to dokeyup
function on that and then i checking by this.attr
to get the typed input.val()
Run snippet below to check all validation is working nicely as you want.