James Lin
James Lin

Reputation: 11

If condition does not validate for both conditions

So I am doing some form validation and I am trying to take user input that only calls the function once both conditions have been met. So far it seems that only the first condition being met will result in a "Works" alert while the second condition does not need to be satisfied. I could input the ~ to try and satisfy the second condition but it will still give me the "Broken" alert. I'm not sure if I can just combine both link1 and link2 into one condition where my function will check for the input to start with http:// but also check for a ~ in the url somewhere.

  var link1 = /[~]/;
  var link2 = /^http:\/\//;

  var input = document.getElementsByName("textbox")[0];
  if(input.value.match(link1 && link2)) {
  alert("Works");
   } else {
  alert("Broken");
   }

Upvotes: 0

Views: 542

Answers (2)

Danyal Sandeelo
Danyal Sandeelo

Reputation: 12391

input.value.match takes one argument, you cannot pass 2 values at the same time in it. you will need to have them checked separately like this:

  if(input.value.match(link1) && input.value.match(link2) ) 

Upvotes: 1

Bharath
Bharath

Reputation: 1

Check like this. it would meet if the both conditions are true

     if(input.value.match(link1) && input.value.match(link2)) {
        alert("Works");
     }
     else {
        alert("Broken");
     }

Upvotes: 0

Related Questions