Using javascript regex containing '@' in razor

前端 未结 4 874
北海茫月
北海茫月 2020-12-10 01:58

I am performing email address validation with javascript in a razor view page. The regex I am going to use is similar to the one proposed at Validate email address in JavaS

相关标签:
4条回答
  • 2020-12-10 02:29

    Simply just use double @@.
    It works for me and I think that is the only way.

    0 讨论(0)
  • 2020-12-10 02:30

    Put the following inside the regEx instead of @:

    @('@')
    

    The above will be rendered to a single @.

    Your example will become:

    /^...otherpart @('@')* ... other part$/
    
    0 讨论(0)
  • 2020-12-10 02:33

    You can add another @ in front of it to escape @@, try leaving out the quantifer *. If this doesn't seem to work then add <text></text> around the function, it tells Razor to not parse the contents. Alternatively you can put Javascript in a separate file to accomplish your needs.

    If for some reason you have multiple @@ in your string, place code blocks ahead @:@@

    0 讨论(0)
  • 2020-12-10 02:33

    It's better to use @('@') instead of @@ (not working for me). Example

    <input class="form-control" pattern="^[a-zA-Z@('@')]{5,20}$"/>
    
    0 讨论(0)
提交回复
热议问题