Take the 2-minute tour ×
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It's 100% free, no registration required.

I added below script to validate url in my form field. It shows valid url.Even if i use magento default class validate-url It won't show error.

$url=https://www.google.co

if(!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$url))
{
  echo "invalid url";
}
else
{
   echo "valid url";
}

How to fix this issue.

share|improve this question
    
I am a bit confused as to what you are trying to achieve here. The url is valid from what I can see so both the validation.js and your regex will return that it is valid. What is the problem that you are looking to solve? –  David Manners Jun 20 at 7:00
    
this is www.google.co invalid url. but it shows valid url.Is there any possibilities in the above method. –  saravanavelu Jun 20 at 7:03
add comment

1 Answer 1

The regex you have posted does not really do what you think it does. If you post this into a regex tool like:

  1. http://www.regexr.com/
  2. http://regex101.com/

Then you will be able to debug your regex, but basically what you have is a match for either http:// or www. so in your case www.google.co is perfectly valid.

Magento's js/prototype/validation.js offers two different url validation options.

  1. validate-url - which requires either http:// https:// or ftp://
  2. validate-clean-url - which will work without the protocol,

So to make your regex work with the protocol you can use the following:

\b(?:(?:https?|ftp):\/\/(?:www\.)?)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]

But be warned that this will also match http://a which I guess you do not want it to do.

share|improve this answer
    
ok.Thankyou.... –  saravanavelu Jun 20 at 7:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.