Skip to content Skip to sidebar Skip to footer

Regexp Validate First Http

I need a regex for removing first http from my input box. how can i do this ? For example if value of my input box is http://welcome.com it remove http:// and convert it to welcom

Solution 1:

Do you really need regular expressions? indexOf() can do the job:

functionCheckLink()
{
    var token = "http://";
    var link = "www.example.com";

    if(link.indexOf(token)!=0)
        link = token + link;

    returnlink;
}

Solution 2:

You don't need to know how to do this, because you should avoid adding http or https to your links.

Use <a href="//www.example.com">click me</a> instead.

See: http://google-styleguide.googlecode.com/svn/trunk/htmlcssguide.xml

Solution 3:

HTML

<inputtype="text" value="www.example.com"id="txt_url" />​

JS

var el=document.getElementById('txt_url');
el.onblur=function()
{
    var str=el.value;
    if(str=='') return;    
    if(str.indexOf('http://')==-1) 
    el.value='http://'+str;    
}

Demo.

Solution 4:

I think you want something like this:

onkeyup="if(!this.value.match(/^http/) )this.value='http://'+this.value"

Post a Comment for "Regexp Validate First Http"