user712410
user712410

Reputation:

Check domain is valid Node.js

In Node.js, how can I check if a domain issued by the user is possible and contains allowed characters only?

I do not want to check if the actual domain is existent, only that it is syntactically correct.

Eg. something.something.something should be allowed, where "*)()-.net shouldn't.

I have tried to use some of the regexs on the question How to validate domain name in PHP? however, I'm actually unsure of how to use these in node. They seemed to always come out false.

Upvotes: 0

Views: 4578

Answers (2)

Hemã Vidal
Hemã Vidal

Reputation: 1882

The npm package validator would be the best choice and a trustable project:

var validator = require('validator');

validator.isURL('google.com', { require_valid_protocol: false }); //=> true
Package: validator
Weekly Downloads
4,309,787 (today)

Upvotes: 3

Emil Condrea
Emil Condrea

Reputation: 9973

Try something like

var reg = new RegExp("[^a-z0-9-.]","i");
reg.test("asdasd.com")//returns false, invalid characters not found
reg.test("asd(.com")//returns true ,invalid characters found 

Upvotes: 1

Related Questions