Archive

Posts Tagged ‘php url form validation’

Regular Expression to Validate URLs

February 10th, 2010 No comments

Here is a short regexp that is used to validate whether a user has entered correct URL address. Might be useful in any scripts dealing with user data.

I will show it as PHP code:

preg_match(‘/^(http:\/\/|https:\/\/)([^\.\/]+\.)*([a-zA-Z0-9])([a-zA-Z0-9-]*)\.([a-zA-Z]{2,4})(\/.*)?$/i’, $_POST['url']);

It will check whether url suits the pattern. May not be ideal, but it’s working. :)