1

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
  • do you wish to catch any protocol specifier or just those in particular? Commented Jun 11, 2010 at 15:42

2 Answers 2

2
function checkURL(textboxval) {
   return ! (/[<>()#'"]|""|(https?|ftp)\:\/\//.test(textboxval));
}
Sign up to request clarification or add additional context in comments.

1 Comment

How to check for case insensitive?
1

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.

4 Comments

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.
Thanks.. This works fine.. It should return false even if the protocol is entered in CAPS.. But it returns true...
If you want it to be case-insensitive, end the regex with /i.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.