简体   繁体   中英

Error with parser recognising regex

I am trying to add a bit of JavaScript validation to my application to check that an email address is formatted correctly.

This is the code that I am using to perform the validation checks:

var bValid = true;
allFields.removeClass( "ui-state-error" );
bValid = bValid && checkLength( email, "email", 6, 80 );
bValid = bValid && checkLength( password, "password", 5, 16 );
bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
var reg = /^(([^<>()[]\.,;:s@"]+(.[^<>()[]\.,;:s@"]+)*)|(".+"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$/;
bValid = bValid && checkRegexp(email, reg, "eg. smith@hotmail.com");
bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
if ( bValid ) {
    //DO ajax call
    $( this ).dialog( "close" );
}

And I am getting this error:

在此处输入图片说明

I have tried numerous examples of regex expressions but all get caught by the parser.

Escape the ] inside character class:

var reg = /^(([^<>()[\]\.,;:s@"]+(.[^<>()[\]\.,;:s@"]+)*)|(".+"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$/;
//            here __^             here __^

Not sure but javascript may want to escape also [ :

var reg = /^(([^<>()\[\]\.,;:s@"]+(.[^<>()\[\]\.,;:s@"]+)*)|(".+"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$/;
//           here __^              here __^

Based on what Chirag64 wrote, it looks like the server might be mistakenly running your JavaScript code? Line 104 is perhaps just the first line where the compiler/interpreter has an error due to it being the wrong language? What programming language is your server expecting?

You should try copying and pasting the problem code into a separate HTML file and see if it will run there when you load it directly into a browser (no server). I suspect that it will.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM