If you want to test for a URL or empty input, you might want to do two passes.
- test for empty string.
- test for valid url.
I would do something like the following (assuming urlString is my input).
// get rid of whitespace, in case user hit spacebar/tab
// also removes leading/trailing spaces.
urlString = urlString.replace(/[\s]*/g,'');
// test if zero length string, if not, test the url.
if( urlString.length > 0 ){ // test the URL
var re = new RegExp( your_expression_goes_here );
var result = re.exec(urlString);
if( result != null ) {
// we have a hit!!! this is a URL.
} else {
// this is a bad string.
}
} else {
// user entered no text, let's move on.
}
So, the preceding should work and allow you to test for either empty string or a url. As to the regular expression you're using "/(http|https):\/\//", I believe it's a bit flawed. Yes, it will catch "http://" or "https://", but it will also key in on a string like "htthttp://" which is clearly not what you want.
Your other sample "/^(http|https):\/\//" is better in that it will match from the beginning of the string and will tell you if the string begins like a URL.
Now, I think jrob above was on the right track with his second string in regards to testing the full URL. I think I found the same sample he used at this page. I've modified the expression as per below and tested it using an online regex tester, can't post the link as I'm a new user :D.
It seems to catch a whole manner of valid URLs and produces an error if the input string is in any way an invalid URL, at least for the invalid URLs I can think of. It also catches http/https protocols only, which I think is your base requirement.
^(?:http(?:s?)\:\/\/|~/|/)?(?:\w+:\w+@)?(?:(?:[-\w]+\.)+([a-zA-Z]{2,9}))(?::[\d]{1,5})?(?:(?:(?:/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|/)+|\?|#)?(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?:#(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)?$
Hope this helps.
Updated code (twice).
I still strongly suggest you test for empty string first as per my earlier example, and you only test for the valid values if the string is non zero. I have tried to combine the two tests into one, but have been unable to do so so far (maybe someone else can still figure it out).
The following tests work for me, here's a URL sample as you required:
//var re = /^(?:http(?:s?)\:\/\/)/;
// the following expression will test for http(s):// and empty string
var re = /^(?:http(?:s?)\:\/\/)*$/;
// use the precompiled expression above, or the following
// two lines:
//var reTxt = "^(?:http(?:s?)\:\/\/)";
//var re = new RegExp(reTxt);
alert(
"result:" + re.test("http://") +
"\nresult:" + re.test("https://") +
"\nresult:" + re.test("") +
"\nresult:" + re.test("https:") +
"\nresult:" + re.test("xhttp://") +
"\nresult:" + re.test("ftp://") +
"\nresult:" + re.test("http:/") +
"\nresult:" + re.test("http://somepage.com") +
"\nresult:" + re.test("httphttp://") +
"\nresult:" + re.test(" http://") +
"\nresult:" + re.test("Random text")
);
And here's a test for dates:
var re2 = /^[0-9]{1,2}\-[0-9]{1,2}\-[0-9]{4}$/;
// use the precompiled expression above, or the following
// two lines:
//var reDateTxt = /^[0-9]{1,2}\-[0-9]{1,2}\-[0-9]{4}$/;
//var re2 = new RegExp(reDateTxt);
alert(
"result:" + re2.test("02-02-2009") +
"\nresult:" + re2.test("022-02-2009") +
"\nresult:" + re2.test("02-032-2009") +
"\nresult:" + re2.test("02-02-23009") +
"\nresult:" + re2.test(" 02-02-2009") +
"\nresult:" + re2.test("02-0a2-2009") +
"\nresult:" + re2.test("02-02-2009") +
"\nresult:" + re2.test("Random text")
);