Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
Merge pull request #701 from LiskHQ/697-url-validator-fixings
Browse files Browse the repository at this point in the history
Fix url validator to accept ip and domain - Closes #697
  • Loading branch information
reyraa committed Sep 4, 2017
2 parents a8525c7 + fd15261 commit bb62be5
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/components/login/login.js
Expand Up @@ -68,15 +68,17 @@ class Login extends React.Component {
return reg.test(url) ? url : `http://${url}`;
};

const isDefaultPort = url => (url.indexOf(':80') || url.indexOf(':443')) !== -1;
const errorMessage = 'URL is invalid';

const isValidLocalhost = url => url.hostname === 'localhost' && url.port.length > 1;
const isValidRemote = url => /(([a-z\d]([a-z\d-]*[a-z\d])*)\.)+[a-z]{2,}|((\d{1,3}\.){3}\d{1,3})/.test(url.hostname);

let addressValidity = '';
try {
const url = new URL(addHttp(value));
const port = isDefaultPort(value) || url.port !== '';
addressValidity = url && port ? '' : 'URL is invalid';
addressValidity = url && (isValidRemote(url) || isValidLocalhost(url)) ? '' : errorMessage;
} catch (e) {
addressValidity = 'URL is invalid';
addressValidity = errorMessage;
}

const data = { address: value, addressValidity };
Expand Down

0 comments on commit bb62be5

Please sign in to comment.