You are viewing limited content. For full access, please sign in.

Question

Question

Javascript OR condition

asked on April 7, 2017

Hello,

I have the following code:

 //Mobile Format for Swiss & Germany
 		$("input[id^=Field14]").on('change blur',function(i, text) {
         if  ($(this).parents("tr").find("[id^=Field85]").val()=="Bern"){
            var text = $(this).parents("tr").find("input[id^=Field14]").val();
      	 	var text = text.replace(/(\d{2})(\d{3})(\d{2})(\d{2})/, "+41" + " "+ "$1 $2 $3 $4");
            $(this).parents("tr").find("input[id^=Field14]").val(text);
		}
        else if  ($(this).parents("tr").find("[id^=Field85]").val()==="Berlin" || ("[id^=Field85]").val()==="Frankfurt"){
      	 	var text = $(this).parents("tr").find("input[id^=Field14]").val();
      	 	var text = text.replace(/(\d{3})(\d{2})(\d{2})(\d{3})/, "+49" + " "+ "$1 $2 $3 $4");
            $(this).parents("tr").find("input[id^=Field14]").val(text);
		}
               
    });
      	
    //Office Phone Format for Swiss & Germany
 		$("input[id^=Field63]").on('change blur',function(i, text) {
      	if  ($(this).parents("tr").find("[id^=Field85]").val()=="Bern" || ("[id^=Field85]").val()=="Zürich" ||("[id^=Field85]").val()== "Lugano" ||("[id^=Field85]").val()== "Genève"){
			var text = $(this).parents("tr").find("input[id^=Field63]").val();
      	 	var text = text.replace(/(\d{2})(\d{3})(\d{2})(\d{2})/, "+41" + " "+ "$1 $2 $3 $4");
           	$(this).parents("tr").find("input[id^=Field63]").val(text);
          
		}
         
      	 else if  ($(this).parents("tr").find("[id^=Field85]").val()=="Frankfurt" ||("[id^=Field85]").val()== "Berlin"){
			var text = $(this).parents("tr").find("input[id^=Field63]").val();
      	 	var text = text.replace(/(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/, "+49" + " "+ "$1 $2 $3 $4 $5");
            $(this).parents("tr").find("input[id^=Field63]").val(text);
		}
 
  });   

What I want to achieve is that if the value is OR between the cities & then the formatting should be applied

 

Please help,

Sahil

0 0

Answer

SELECTED ANSWER
replied on April 7, 2017 Show version history

I have tweaked your code.  I have NOT tested these changes.

//Mobile Format for Swiss & Germany
$("input[id^=Field14]").on('change blur',function() {
	if ($(this).parents("tr").find("[id^=Field85]").val()=="Bern"){
		var text = $(this).parents("tr").find("input[id^=Field14]").val();
		text = text.replace(/(\d{2})(\d{3})(\d{2})(\d{2})/, "+41" + " "+ "$1 $2 $3 $4");
		$(this).parents("tr").find("input[id^=Field14]").val(text);
	}
	else if ($(this).parents("tr").find("[id^=Field85]").val()==="Berlin" || $(this).parents("tr").find("[id^=Field85]").val()==="Frankfurt"){
		var text = $(this).parents("tr").find("input[id^=Field14]").val();
		text = text.replace(/(\d{3})(\d{2})(\d{2})(\d{3})/, "+49" + " "+ "$1 $2 $3 $4");
		$(this).parents("tr").find("input[id^=Field14]").val(text);
	}
});

//Office Phone Format for Swiss & Germany
$("input[id^=Field63]").on('change blur',function() {
	if  ($(this).parents("tr").find("[id^=Field85]").val()=="Bern" || $(this).parents("tr").find("[id^=Field85]").val()=="Zürich" || $(this).parents("tr").find("[id^=Field85]").val()== "Lugano" || $(this).parents("tr").find("[id^=Field85]").val()== "Genève"){
		var text = $(this).parents("tr").find("input[id^=Field63]").val();
		text = text.replace(/(\d{2})(\d{3})(\d{2})(\d{2})/, "+41" + " "+ "$1 $2 $3 $4");
		$(this).parents("tr").find("input[id^=Field63]").val(text);
	}
	else if  ($(this).parents("tr").find("[id^=Field85]").val()=="Frankfurt" || $(this).parents("tr").find("[id^=Field85]").val()== "Berlin"){
		var text = $(this).parents("tr").find("input[id^=Field63]").val();
		text = text.replace(/(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/, "+49" + " "+ "$1 $2 $3 $4 $5");
		$(this).parents("tr").find("input[id^=Field63]").val(text);
	}
});

This is what I changed (when mentioning the row numbers, I'm referring to the code from your original post, not form the code I just posted):

  • Removed i,text from rows 2 and 17 (since they were not used in the functions).
  • Removed var from rows 5, 10, 20, and 27 since the variable was already being declared on the rows prior.
  • Added code (     $(this).parents("tr").find     ) before each of the evaluation criteria on row 8, 18, and 25.
  • And made some formatting changes (because I was using an editor that defaults that).

 

If it doesn't operate as expected, I recommend adding code in various places (i.e.   alert('test');    ) to see where in the code the processing is happening vs. not happening.

0 0

Replies

replied on April 10, 2017

Thanks Mathew!

1 0
replied on April 7, 2017 Show version history

I'm not certain I understand your question.

But, looking at your code, I think I see an issue with your criteria evaluation.

Take line 18 for example: 

if  ($(this).parents("tr").find("[id^=Field85]").val()=="Bern" || ("[id^=Field85]").val()=="Zürich" ||("[id^=Field85]").val()== "Lugano" ||("[id^=Field85]").val()== "Genève"){

The first value you are evaluating is: 

$(this).parents("tr").find("[id^=Field85]").val()=="Bern"

But on the subsequent values, you are only evaluating part of the info: 

("[id^=Field85]").val()=="Zürich"

The part you are dropping off (  $(this).parents("tr").find   ) in the secondary values likely needs to be included in each of the items you are evaluating.

0 0
replied on April 7, 2017

Thanks Mathew,

My desired result is, if the value is either Bern or Zürich or Lugano or Genèva

Than format....

text.replace(/(\d{2})(\d{3})(\d{2})(\d{2})/, "+41" + " "+ "$1 $2 $3 $4");

 

if Berlin or Frankfurt

then the other...

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.