0
votes

Morning, I wanna call the label when a function onkeyup return false, for example. I set jquery validator, and i set a rule clled

e-mail: {
email:true,
required:true
}

but I have another function that validate the email and return false if it is not correct and true if the opposite.

function emailvalidate(email_string){
[regex test emailstring] 
return true or false //(and show label of jqueryvalidator)
}

now, when I write an invalid email Jquery validator shows a label saying email wrong but I need also that on keyup or on focus other field it will test the email form value with my function emailvalidate() and if it returns false it will show the message of the jquery validator plugin. sth like

$("#email-field").on("keyup",emailvalidate($("#email-field").val()))
2

2 Answers

1
votes

well i dont have any idea about your plugin well if you will you can use this js validation code to validate

function ValidateEmail(mail) 
{
 if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(mail))
  {
    return (true)
  }
    return (false)
}
$(document).ready(function() {
$("#email-field").on("keyup",function(e){
    var mail = $("#email-field").val();
    var result = ValidateEmail(mail);
    if(result == true){
        //do something here
    }else{
        //do something here
    }
    return result;
    e.preventDefault();
});

});
0
votes

This was the solution for my problem, just added a new method to my existing validator, with the function.

 jQuery.validator.addMethod("correoval", function(emailAddress) {
                    var pattern = /^([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*(([ \t]*\r\n)?[ \t]+)?")@(([a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.)+([a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.?$/i;
                    return pattern.test(emailAddress);
                }, "Ingrese una dirección de correo válida");

An then the rules for jQueryValidator

$("#MyForm").validate({
                rules: {
                    corrA: {
                        email: true,
                        required: true,
                        correoval:true //this was the method that I added
                    }