JS Regex url validation

后端 未结 4 531
梦毁少年i
梦毁少年i 2021-02-07 12:41

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.

4条回答
  •  深忆病人
    2021-02-07 12:55

    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;
    }
    

提交回复
热议问题