1
I’m developing a system and want to make validations through functions.
I have the function checkUsername
who consults at the bank to return to me whether or not to register with that username.
And then you have other functions to check other things like checkEmail
. How to apply these functions one after the other in form Submit?
Examples of functions I’m using:
function checkUsername(username){
$("#username").focusout(function() {
var username = $("#username").val();
var pattern = /^[a-z0-9_-]{3,16}$/;
if (pattern.test(username)) {
var Urlverificadora = 'action=verificar_username&username='+username;
$.ajax({
type : 'POST',
data : Urlverificadora,
url : painel+'checkUser.php',
success: function(responseText){ // Get the result and asign to each cases
if(responseText == 0){
$("#username").css("border", "2px dashed green");
} else if(responseText > 0){
$("#username").css("border", "2px dashed red");
} else{
alert('Entre em contato com o administrador.');
} //success
}
}); //ajax
} else {
$("#username").css("border", "2px dashed red");
};}
)};
Interesting your question, you can control Submit how to disable until all validations are correct. In the specific case of the user, it would be good if you listened to the changes through an event. So, the user would already know if he has the right username before submitting the form.
– Ronny Amarante
#Ronny Amanrante (@Ronny Amanrante), could you demonstrate this in code so I know how to structure the validations and how to call the script through Submit? Since there are functions, I do not know how to apply them in cascade form!
– Marcos Vinicius