Hello Reader's if you have implemented multiple input for emails and you want to validate all of them using a single validation rule. By using JQuery you can make this.
Lets see how to make validation:-
Suppose your html page is like this:-
<select name="to[]" id= "validatethisEmail" class="js-example-tokenizer select2-hidden-accessible form-control" multiple="" tabindex="-1" aria-hidden="true" required>
<?php foreach ($Email[0]['send_to'] as $to) { ?>
<option value="<?php echo $to; ?>" selected =""> <?php echo $to; ?>
</option>
<?php } ?>
</select>
Now the validation part for this html code will go like this:-
<script>
function validateEmail()
{
var values = "";
values = $.map($('#validatethisEmail option'), function(e) { return e.value; });
values.join(',');
console.log(values);
console.log(values.length);
var regexEmail =/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
for (i = 0; i < values.length; i++) {
if (regexEmail.test(values[i]) == false)
{
$("#values").html(values[i] + 'is an invalid email');
alert(values[i] + 'is an invalid email');
$("#createForm").submit(function(e){
e.preventDefault();
});
}else{
$('#createForm').unbind('submit');
$("#values").html("");
}
}
values = "";
};
</script>
In this script submit button will send all the emails inside the validateEmail() function. There it will explode and every single value will check against the email format. If its found incorrect, it will stop the form to submit and print the invalid email on screen.
0 Comment(s)