tags:

views:

31

answers:

2

All,

I have following function to check for invalid symbols entered in a text box and return true or false. How can I modify this function to also check for occurrences like http:// and https:// and ftp:// return false if encountered ?

function checkURL(textboxval) {
   return ! (/[<>()#'"]|""/.test(textboxval));
}

Thanks

+1  A: 

You want it to return false if it encounters a protocol?

function checkURL(textboxval) {
    return ! (/[<>()#'"]|""|(f|ht)tp(s)?:\/\//.test(textboxval));
}

This is a useful tool for figuring these things out also: RegexPal.

Andy
I like the fact that `(f|ht)tp(s)?` the same length, but much harder to read and less efficient than `(https?|ftp)`… ;-) If you take non-capturing groups it even gets *longer* than the straight-forward variant.
Tomalak
Thanks.. This works fine.. It should return false even if the protocol is entered in CAPS.. But it returns true...
Vincent
If you want it to be case-insensitive, end the regex with /i.
Andy
Tomalak, you're correct. I started with just `http` and then was mucking around in the tester and forgot to clean it up before posting here.
Andy
+2  A: 
function checkURL(textboxval) {
   return ! (/[<>()#'"]|""|(https?|ftp)\:\/\//.test(textboxval));
}
Kyril
How to check for case insensitive?
Vincent