I have added jQuery validator add method for the presence of http check 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
"unterminated regular expression literal" error in the following line.
if(!/^(https?|ftp):\/\/i.test(val))
What my mistake is?
Regular expression literals should be surrounded by the delimiters(/
). There's no terminating delimiter:
/^(https?|ftp):\/\//i
// ^
For example:
>> /^(https?|ftp):\/\//i.test('http://stackoverflow.com/')
true
>> /^(https?|ftp):\/\//i.test('telnet://stackoverflow.com/')
false
You can also get this error if you're simply attempting to concatenate a variable that utilizes a regular expression and forget a quotation mark somewhere.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With