Angular 2 : Validators.pattern() not working

前端 未结 8 2104
醉酒成梦
醉酒成梦 2020-12-08 19:54

I am trying to validate input type=\"text\" using a pattern. I want text only.

Component :

 this.from = this.fb.group({
  name: [\'\',[         


        
相关标签:
8条回答
  • I had this same problem with a different pattern:

    ^\d{1,4}$
    

    And I was using it like this:

    Validators.pattern("^\d{1,4}$") // wrong
    

    The problem is that the backslash \ has to be escaped, so the correct form is:

    Validators.pattern("^\\d{1,4}$") // correct
    
    0 讨论(0)
  • 2020-12-08 20:26

    Remember to not do this:

    Validators.pattern("[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}")
    

    The gotcha is that you need a double backslash before the s to defeat string escape thus:

    Validators.pattern("[A-Z]{1,2}[0-9][0-9A-Z]?\\s?[0-9][A-Z]{2}")
    
    0 讨论(0)
提交回复
热议问题