I have searched a lot but didn't get the answer.
How to validate URLs like www.google.com and http://www.google.com
using regular expressions? 
Thanks in advance.
            Asked
            
        
        
            Active
            
        
            Viewed 8,894 times
        
    1
            
            
         
    
    
        Wiktor Stribiżew
        
- 607,720
- 39
- 448
- 563
 
    
    
        manoj
        
- 505
- 9
- 29
- 
                    What do you mean by validate an URL? Check if it starts with "http://", or "www": `^(?:http|www)`. Have you tried anything? – Wiktor Stribiżew Apr 01 '15 at 10:43
- 
                    it could be starting from both "www" or "http://" – manoj Apr 01 '15 at 10:45
2 Answers
2
            You can use a function to test valid url as:
function validateUrl()   // return true or false.
{
    var urlregex = new RegExp(
          "^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)");
    return urlregex.test(textval);
}
 
    
    
        svlasov
        
- 9,923
- 2
- 38
- 39
 
    
    
        Umesh Sehta
        
- 10,555
- 5
- 39
- 68
- 
                    your solution not working at all. see this fiddle http://jsfiddle.net/KJW4E/1284/ – Jitendra Pancholi Apr 05 '18 at 09:14
- 
                    
1
            
            
        You can also use this one, that does not depend on the string start/end:
(\b((?:https?|ftp):\/\/|www\.)([0-9A-Za-z]+\.?)+\b)
See example here.
 
    
    
        Wiktor Stribiżew
        
- 607,720
- 39
- 448
- 563