Jake
Jake

Reputation: 26117

JavaScript Regex question

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

Upvotes: 1

Views: 53

Answers (2)

Kyril
Kyril

Reputation: 3102

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

Upvotes: 2

Andy
Andy

Reputation: 14184

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.

Upvotes: 1

Related Questions