1

I need to run validation on one of the form fields in my app. For this, I am using ReactiveFormsModule

My pattern as follows:

domainPattern = '^(((?!-))(xn--|_{1,1})?[a-z0-9-]{0,61}[a-z0-9]{1,1}\.)*(xn--)?([a-z0-9\-]{1,61}|[a-z0-9-]{1,30}\.[a-z]{2,})$';

I am using the above pattern in my pattern Validator code as follows:

this.customerForm = this.formBuilder.group( {
      name: [ '' ],
      company: [ '' ],
      email: [ '', [ Validators.required, Validators.email ] ],
      domain: [ '', [ Validators.required, Validators.pattern( this.domainPattern ) ] ]
    } );

For email it's standard working well. But for the custom domain regex doesn't work for me. I have used regex from here.

Based on the questions reviewed it seems difficult to find general domain name regex for all of the valid domains but I want to cover most basic cases.

The current pattern shows: $#$#.com, abcd as valid domains.

Do Validators have any constant validator similar to Validators.email for the domain?

Prashant
  • 3,934
  • 4
  • 30
  • 65
  • `abcd` is a valid domain (it doesn't exist, although `abc` does, but it is valid) – Quentin Jun 04 '19 at 09:56
  • _Do Validators have any constant validator similar to Validators.email for the domain?_ No – Jota.Toledo Jun 04 '19 at 09:57
  • You are missing literal backslashes in your regex, use `domainPattern = /^(((?!-))(xn--|_)?[a-z0-9-]{0,61}[a-z0-9]\.)*(xn--)?([a-z0-9-]{1,61}|[a-z0-9-]{1,30}\.[a-z]{2,})$/` – Wiktor Stribiżew Jun 04 '19 at 09:57

0 Answers0