ValidatorOptional

url

Check a URL, and optionally its scheme.

Installation

Import the class, and @use the entry point in your global styles: without it the component renders unstyled.

import { Component } from '@angular/core';
import { WrInput } from 'ngwr/input';
import { WrValidators } from 'ngwr/validators';

@Component({ imports: [WrInput] })
export class MyComponent {}

Live demo — any scheme

errors: null

Live demo — HTTPS only

errors: null

Usage

A scheme is required. url({ requireProtocol: false }) also accepts ngwr.dev.

import { FormControl } from '@angular/forms';
import { WrValidators } from 'ngwr/validators';

const link = new FormControl('', [WrValidators.url()]);

const secure = new FormControl('', [
  WrValidators.url({ protocols: ['https'] }),
]);
secure.setValue('http://example.com');
secure.errors; // { url: { allowed: ['https'] } }

API

NameDescriptionTypeDefault
signatureCall it to get a ValidatorFn.(options?: { protocols?: readonly string[]; requireProtocol?: boolean }) => ValidatorFn—
options.protocolsAccepted schemes, any case, with or without :.readonly string[]any scheme
options.requireProtocolWhen false, ngwr.dev passes, read as https://ngwr.dev.booleantrue
error keyNot a URL: { url: true }. Wrong scheme: { url: { allowed: [...] } }. Empty value passes.{ url: true | { allowed: string[] } }—

See also