Javascript help: How do I validate a URL entered into a form?

2 replies
Hi folks,

I've been battling this one for too many hours...it's time to ask for help. I'm trying to combine a few free scripts because I can't find one that will do exactly what I want.

Here's the offending page WebsiteMobiliser.com | Mobile Website Portfolio | Australia

What I'm trying to do is display a website in a frame. The website comes from user input into a form field. It currently works OK unless the user forgets to enter http://.

I'll use this forum url as an example:

It works fine for http://www.warriorforum.com

and it will work fine for http://warriorforum.com

but will not work for warriorforum.com or www.warriorforum.com

or warriorforum.com

Here's what I have used in the head...

Code:
<script type="text/javascript">
var url;
function submitURL() {
 url = document.getElementById("goTo").value;
 check = url.substr(0,7);
 check = check.toLowerCase();
  if (check != "http://")
  {
    url = "http://"+url;
    myURL.document.location.href = url;
    }
 else
    {
  myURL.document.location.href = url;
    }
 }
</script>
and here is the code for the form...

Code:
   <p>
    <form target="frame" onsubmit="this.action=this.goTo.value">
     <b><font face="arial">Test your site...</font></b><br />
    <input type="text" name="goTo" onchange="submitURL()" size="50" id="goTo" placeholder="enter your web address here"></form>
     <font face="arial" size="-1">(enter your website, including the http://,  press enter)</font>
   </p>
I have called my iframe "frame" in this iframe code...

Code:
    <iframe name="frame" style="height: 489px; width: 327px;" src="Website Mobiliser | Mobile Website Designer" frameborder="0" marginwidth="0" scrolling="no" width="320" height="240">
    </iframe>
Special brownie points will go to someone who can help me add a button to the form so I don't have to ask them to press enter.

I'd appreciate any suggestions even if you're not sure if it will work. I'm willing to try things until I figure this out. If I figure it out I'll post the solution so we can all learn.

Tips: I think the problem is in the naming of certain elements. The javascript code I got from a coding website and I'm pretty sure it needs adjusting to point to my frame but I'm not sure where. Is it in the "goTo" or the "myURL" parameters or elsewhere or am I way off? I know I'm close but I just don't have quite enough skill to pull this off.
#entered #form #javascript #url #validate
  • Profile picture of the author Tradeout
    Bill I've dropped you a PM about this.
    {{ DiscussionBoard.errors[6360039].message }}
  • Profile picture of the author Mr Bill
    Cool, I appreciate it.
    {{ DiscussionBoard.errors[6360048].message }}

Trending Topics