Unterminated regular expression literal js error

I have added jQuery validation method to have HTTP validation in url field.

Js

jQuery.validator.addMethod("valid_url", function(value, element) {
 if(!/^(https?|ftp):\/\/i.test(val))
    val = 'http://'+val; // set both the value
    $(elem).val(val); // also update the form element
 }

My console throws an
"uninitialized regular expression literal in the next line.

if(!/^(https?|ftp):\/\/i.test(val))

What is my mistake?

+4
source share
1 answer

Regular expression literals must be surrounded by delimiters ( /). No limiter:

/^(https?|ftp):\/\//i
//                 ^

For example:

>> /^(https?|ftp):\/\//i.test('http://stackoverflow.com/')
true
>> /^(https?|ftp):\/\//i.test('telnet://stackoverflow.com/')
false
+3
source

All Articles