-1

I have function check dynamic input fields with a certain class. Those fields takes email address so checking if it is a valid email address.

There could be many email field, if one of them fails against email address test, it should return FALSE, else it should return TRUE. Finally when submit clicked save_form() runs. If it get false, it should stop, otherwise continue. But it gets "undefined" always.

Here is what I have:

function validate_email_string(email_address) {
  var emailReg=/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
  var emailTest = emailReg.test(email_address);
  var result = (emailTest === false) ? false : true;
  return result;
}

function validate_email_extensions(email_field_selector, message) {
  var invalid_message = null;
  if (typeof message !== "undefined") {
    invalid_message = message;
  } else {
    invalid_message = '<?=lang("i18n_invalid_email_address");?>';
  }
  var email_fields_length = $(email_field_selector).length;
  var is_valid = true;
  if (email_fields_length) {
    $(email_field_selector).each(function(index, el) {
      if($(el).val()) {
        if(!validate_email_string($(el).val())) {
          is_valid = false;
        }
        if (index === email_fields_length - 1) {
          if (is_valid === false) {
            Interact.show(invalid_message);
          }
          return is_valid;
        }
      } else {
        if (index === email_fields_length - 1) {
          if (is_valid === false) {
            Interact.show(invalid_message);
          }
          return is_valid;
        }
      }
    });
  } else {
    return is_valid;
  }
}

function save_form() {
    var emails = validate_email_extensions('.jq-extension-email');
    if(!emails){return false}
    ....

But validate_email_extensions function returns undefined. I put console.log() everywhere, probably it's blind of me but any help is quite appreciated.

YahyaE
  • 1,057
  • 1
  • 9
  • 24
  • @Hanky Panky First `if` is not supposed to return anything :) It is just for setting a default message. Thanks for contribution anyway. – YahyaE Mar 25 '16 at 09:25
  • 1
    Yeah i didnt mention correctly which first then i realized that can be ambiguous so i removed the comment and put it in an answer with different content – Hanky Panky Mar 25 '16 at 09:26

1 Answers1

3

Your returns are from your loop to validate_email_extensions, not from validate_email_extensions to save_form as you're expecting.

validate_email_extensions needs to return the value to save_form still.

The only time where your function is correctly returning a value is when this case is FALSE

if (email_fields_length) {

It returns no value when control goes in that if condition.

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • `is_valid` defined before this statement. So the idea is to set `is_valid` as `true` by default. During each loop, set it `false` if validation fails. And after the last loop, check if `ìs_valid` is `false`. If it is, it means at least one field is failed from validation. So I think it must be ok when you notice the rest of the code. Though, If I'd be an expert, I wouldn't ask here so I might be wrong :) – YahyaE Mar 25 '16 at 09:30
  • Defining something means nothing. You're not `returning it` to the caller and that is why for caller it is undefined. You can define a whole encyclopedia in a function but if you don't properly send it back to the caller (or make it global) it is out of scope for the caller. – Hanky Panky Mar 25 '16 at 09:30
  • Yes, because I am returning it if it is the last loop of each which is just the below line. – YahyaE Mar 25 '16 at 09:32
  • Whatever you're returning is returning a value to function from your loop closure. That value still remains in your function. You still have to return it from here `validate_email_extensions`. The returns that are there currently are only from your loop to `validate_email_extensions` and *not* from `validate_email_extensions` to `save_form` – Hanky Panky Mar 25 '16 at 09:35
  • I think I get the idea. I did it this way to make sure that I will send it after loop completed. But as you say, I am returning a value from loop to function, not from function to caller. – YahyaE Mar 25 '16 at 09:38
  • 1
    Yeah after 50 revisions haha :) – Hanky Panky Mar 25 '16 at 09:39
  • I am a (re)Visioner :) Sorry for revisions by the way. Thanks for help. – YahyaE Mar 25 '16 at 09:41