Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parsing @ in MVC javascript section? [duplicate]

I am trying using Email RegEx in javascript section in MVC4. But RegEx has @ char. It is not allowing to parse it

error:

Parser Error Message: "[" is not valid at the start of a code block.  
        Only identifiers, keywords, comments, "(" and "{" are valid.

code

@section scripts{
    <script type="text/javascript">
        $(document).ready(function() {

            $('#btnSave, #btnCoAuthor').click(function() {
                if (form.valid()) {
                    var hasError = false;
                    var emailReg = '[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4}';
                      //Error showing @
like image 453
James123 Avatar asked Jun 05 '14 16:06

James123


1 Answers

You need to escape the @ for Razor, not for the JS string, itself. So, just use @@. Once Razor renders the HTML, it will end up as just an @.

like image 78
Chris Pratt Avatar answered Oct 02 '22 16:10

Chris Pratt