0

I have the following HTML/jQuery code that generates some links.

HTML

<input type="text" id="text" placeholder="Enter text" autofocus />
<a id="link1" class="btn btn-info" href="#" target="_blank"> Search 1 </a>
<a id="link2" class="btn btn-info" href="#" target="_blank"> Search 2 </a>
<a id="link3" class="btn btn-info" href="#" target="_blank"> Search 3 </a>

jQuery

<script>
    window.onload = function(){
        var sel = document.getElementById('text');
        sel.onchange = function () {
            document.getElementById("link1").href = "http://example.com/action?queryString=" + this.value;
            document.getElementById("link2").href = "http://example.com/searchall.php?search=" + this.value;
            document.getElementById("link3").href = "http://example.com/dashboard.aspx?term=" + this.value;
            document.getElementById("text").focus();
        }
    };
</script>

On the 2nd search link, I'm looking to implement a change wherby if the value entered is an IP address, then the URI is changed. e.g.

document.getElementById("link2").href = "http://example.com/ipsearch.php?ip=" + this.value;

I just don't have a clue on where to start with this change.

Any helps would be greatly appreciated.

1 Answer 1

1

Perhaps something like this:

sel.onchange = function () {
  var value = this.value;
  var isIP = /^(([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)\.){3}([1-9]?\d|1\d\d|25[‌​0-5]|2[0-4]\d)$/.test(value);
  if (isIP) {
    document.getElementById("link2").href = "http://example.com/ipsearch.php?ip=" + value;
  } else {
    document.getElementById("link2").href = "http://example.com/searchall.php?search=" + value
  }
};

This uses a regex to determine if an IP was entered, and if it was sets a different URL.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.