0

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.

Community
  • 1
  • 1

2 Answers2

2

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)
Hemã Vidal
  • 1,742
  • 2
  • 17
  • 28
0

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 
Emil Condrea
  • 9,705
  • 7
  • 33
  • 52
  • 1
    Thanks! The problem was actually somewhere else in my program, but thanks anyway. –  Nov 03 '13 at 16:31