Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
10
rated 0 times [  15] [ 5]  / answers: 1 / hits: 86276  / 9 Years ago, sun, june 21, 2015, 12:00:00

I tried to validate url with or without http No matter what i did the function return false.
I checked my regex string in this site:
http://regexr.com/
And its seen as i expect.



    function isUrlValid(userInput) {
var regexQuery = /(http(s)?://.)?(www.)?[-a-zA-Z0-9@:%._+~#=]{2,256}.[a-z]{2,6}b([-a-zA-Z0-9@:%_+.~#?&//=]*)/;
var url = new RegExp(regexQuery,g);
if (url.test(userInput)) {
alert('Great, you entered an E-Mail-address');
return true;
}
return false;
}


I fix the problem by change the .test to .match and leave the regex as is.


More From » jquery

 Answers
43

I change the function to Match + make a change here with the slashes and its work: (http(s)?://.)



The fixed function:



function isUrlValid(userInput) {
var res = userInput.match(/(http(s)?://.)?(www.)?[-a-zA-Z0-9@:%._+~#=]{2,256}.[a-z]{2,6}b([-a-zA-Z0-9@:%_+.~#?&//=]*)/g);
if(res == null)
return false;
else
return true;
}

[#66107] Friday, June 19, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
quentinaveryb

Total Points: 102
Total Questions: 100
Total Answers: 93

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
quentinaveryb questions
Thu, Aug 6, 20, 00:00, 4 Years ago
Fri, Jul 17, 20, 00:00, 4 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;