Check if string is a URL?


#1

How do you check if a string contains http:// or not? Thanks :slight_smile:


#2
How do you check if a string contains http:// or not? Thanks :)
if (myString.split("http://").length > 1) { // is an url... }

:slight_smile:


#3

Or use indexOf() to get same result:

if (myString.indexOf("http://")>-1) {
	trace("This string has a link");
}

#4

And to be sure that the string starts with “http://”


if (myLink.split("http://")[0] == "http://") {}

or

if (myLink.indexOf("http://") == 0) {}

:slight_smile:


#5

ok thanks!

so what is length > 1… the length of the remaining bit where it split the string at “http://” ?


#6
ok thanks!

so what is length > 1… the length of the remaining bit where it split “http://” ?

The split function returns an array, so if you have:

myString = "a,b,c,d,e";

if you do 

myString.split(",") you will get an array of 5 elements, so the length will be 5

if you do

myString.split("hello") the length of the array will be 0 because the separator won't be found

#7

So basically if the url is valid you will get with the split an array with 2 elements.

At the index 0 you will have the “http://” and at the index 1 you will have the other part of the url.


#8

Intresting, I used to use VF’s method IndexOf
but ParkerAndKent method is more advanced
thanks


#9
Intresting, I used to use VF's method IndexOf but ParkerAndKent method is more advanced thanks

Thanks, but it isn’t more advanced… we just use two functions that have different purposes but that give the same result in a case like this one :wink:


#10

I also use indexOf split creates a array and you don’t need a array for this :slight_smile:


#11
Intresting, I used to use VF's method IndexOf but ParkerAndKent method is more advanced thanks

Thanks, but it isn’t more advanced… we just use two functions that have different purposes but that give the same result in a case like this one :wink:

Thats what i meant with more advanced. It can be used in more complex ways, lets say for validating an email input box. That way we could prevent users from starting with “@” at the beginning. In indexOf() this cant be achieved , it only checks if “@” is available or not


#12
I also use indexOf split creates a array and you don't need a array for this :)

Well, you cannot say that… if you want to check the validity of an url indexOf isn’t enough…

If you have for example only “http://” with the split it will never verify the length > 1 while with the indexOf it will consider it a correct link in both cases

== 0 or >-1

#13
var isURL: Boolean = myString.substr(0, 7) == "http://" && myString.length > 7;

Best of both worlds :slight_smile:


#14

Though to be more bulletproof I’d go a little more in depth:

http://pastie.org/412914


#15
In indexOf() this cant be achieved , it only checks if "@" is available or not

indexOf() returns the exact index in the string of where the first instance of the given string exists, so yes you can :slight_smile:


#16
Though to be more bulletproof I'd go a little more in depth:

http://pastie.org/412914

LOL good :smiley:


#17

I didn’t know that,
an extra info, thanks


#18

Does anyone else have the nagging in the back of their head that we are just helping DS make his next project that is going to make us all look like little children trying to use flash… :stuck_out_tongue:


#19

And in as3 using the regular expressions it would be also easier and faster :slight_smile:

“Link”:http://www.flashcomguru.com/index.cfm/2009/2/17/regexp-validate-url


#20
Does anyone else have the nagging in the back of their head that we are just helping DS make his next project that is going to make us all look like little children trying to use flash... :P

:frowning: