efarley
efarley

Reputation: 8681

Angular 2 RC.6 form input patten never passes

I am trying to make it so that the required input in my form doesn't pass validation unless there's at lease one non-whitespace character in the value, however for some reason the validation never passes. Am I missing an import or something?

Template:

<form #createForm='ngForm'>
    <input type='text' required pattern='[/S]+' name='projectName' [(ngModel)]='projectName' placeholder='Give your project a name...' class='focusOnMe'>
</form>

Component:

import {
  Component,
  ElementRef
} from '@angular/core';

Component({
  selector: 'cmg-modal-create',
  template: require('./modals.create.html')
})

export class ModalCreateComponent extends CreateModal {
    ...
}

Module:

@NgModule({
  declarations: [
    ModalCreateComponent
    ...
  ],
  exports: [
    ...
  ],
  imports: [
    CommonModule,
    FormsModule,
    ...
  ]
})

Upvotes: 0

Views: 83

Answers (2)

efarley
efarley

Reputation: 8681

Looks like the regex just didn't work like I expected. I ended up with this pattern which says that the first character can't be a whitespace. I'd like to update it do that the first character can be a whitespace as long as there is another character after it, but I've been unable to figure that out.

<input type='text' name='projectName' [(ngModel)]='projectName' required pattern='[^\s][\W\w]+' placeholder='Give your project a name...' class='focusOnMe'>

Upvotes: 0

Yakov Fain
Yakov Fain

Reputation: 12376

Take a look at this code sample with custom validation: https://github.com/Farata/angular2typescript/blob/master/chapter7/form-samples/app/06_custom-validator-directive.ts

Upvotes: 0

Related Questions