2

I have the following function:

jQuery CODE:

function validarEnviarPatrocinio(){

        var fila = $("#fila").val();

        for(var e=1; e<6; e++){
            $("#email_contactopatrocinador"+e).val($("#email_contactopatrocinador"+e+fila).val());
            var email = $("#email_contactopatrocinador"+e+fila);                
        }           
 }

PHP/HTML CODE:

<?php foreach ($this->items as $i => $item): ?>

<?php for($e=1; $e<6; $e++){ ?>
    <label for="email_contactopatrocinador<?php echo $e.$i; ?>">
        <?php echo JText::_('COM_CSTUDOMUS_GANA_PATROCINADOR_EMAIL');?>
    </label>

<input name="email_contactopatrocinador<?php echo $e.$i; ?>" id="email_contactopatrocinador<?php echo $e.$i; ?>" placeholder="<?php echo JText::_('COM_CSTUDOMUS_EJEMPLO_EMAIL');?>" type="email" />
<?php } ?>

<button id="enviarForm2" onclick="document.getElementById('cb<?php echo $i; ?>').checked = true; document.getElementById('seleccionado').value = <?php echo $item->id_inmueble; ?>;  document.getElementById('fila').value = <?php echo $i; ?>; if(!validarEnviarPatrocinio()) return false; Joomla.submitbutton('saloninmobiliarios.ganapatrocinador');" class="uk-button uk-float-right"><?php echo JText::_('COM_CSTUDOMUS_PATROCINADOR_SEND_RECOM'); ?></button>

<?php endforeach; ?>

<input type="hidden" name="fila" id="fila" value=""/>

This function loops through 5 input fields which are "email" type. I want to make a validation for each input making sure they insert an email format inside. It shouldnt matter which field they fill in or leave blank.

Victor York
  • 1,621
  • 4
  • 20
  • 55
  • possible duplicate of [Validate email address in JavaScript?](http://stackoverflow.com/questions/46155/validate-email-address-in-javascript) – Ram Mar 26 '14 at 12:34
  • @undefined this is not a duplicate, im asking "How to validate each value inside a jquery object" so afterwards I can validate each of those fields but I first need to know how to manage a jquery obect. – Victor York Mar 26 '14 at 12:36
  • It is nearly impossible to check if an email address is valid short of sending an email to that address. Are there some specific rules you had in mind as to what constitutes an email? – user3334690 Mar 26 '14 at 12:36
  • "_How to validate each value inside a jquery object_", what does that even mean? Your question is so vague, the key point of your question is validating email addresses. Are you having problem with selecting the target elements? What is the `fila`? – Ram Mar 26 '14 at 12:40
  • @undefined Im having a problem selecting targeted elements.. sorry for my English :( – Victor York Mar 26 '14 at 12:47
  • Please post the related HTML and provide more context. Just a note, there is no need to use IDs for selecting the elements, you can add a class to the elements and iterate through the selected elements using jQuery `each` method. – Ram Mar 26 '14 at 12:50

3 Answers3

2

You are making it complex. There is no need to use IDs for selecting the target elements, when you have several elements that share the same role, classes should be used instead. You can add classes to elements and iterate through the selected elements using jQuery each method:

$('.email_fields').each(function() {
    var email = this.value;
    // validate the element's value
});

If there is a need for adding unique identifier to the elements, you can continue adding id attributes or a specific data attribute like data-id="..." to the elements and for getting the value of the data attributes, you can use the jQuery data method.

// within the `each` callback context
var id = $(this).data('id');
Ram
  • 143,282
  • 16
  • 168
  • 197
0
    function validarEnviarPatrocinio(){

            var fila = $("#fila").val();

            for(var e=1; e<6; e++){
                $("#email_contactopatrocinador"+e).val($("#email_contactopatrocinador"+e+fila).val());
                var email = $("#email_contactopatrocinador"+e+fila); 
                if ($.trim(email ).length == 0) {
                alert('Please enter valid email address in '+i+'th Field ');

            }
            if (validateEmail(email)) {
                alert('Email'+i+' is valid');
            }
            else {
                alert('Invalid Email'+i+' Address');               
            }           
     }
function validateEmail(sEmail) {
    var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    if (filter.test(sEmail)) {
        return true;
    }
    else {
        return false;
    }
}
Tuhin
  • 3,335
  • 2
  • 16
  • 27
0

You can use jquery validator and insert a reg expresion to validate. the validator gets as an input the selector and validates them with sets of rules you define as follows:

$('form').validate({
      rules: {
            field1: {pattern: /^[A-Z]{2}[0-9]{5}$/} // As RegExp
      }
});

*The relevant regEx for your case is: /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/

in any case you can also create regex in js and run the test function as described here:

Community
  • 1
  • 1
Zutta
  • 1
  • `validate` plugin has `email` validator, there is no need to specify a pattern, `field1: { email: true }`. – Ram Mar 26 '14 at 12:54
  • very much true. I wrote it down in case he wants more generic/complicated validation than ordinary email – Zutta Mar 26 '14 at 13:20