1

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:

enter image description here

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

isaias-b
  • 2,255
  • 2
  • 25
  • 38
brian4342
  • 1,265
  • 8
  • 33
  • 69

2 Answers2

2

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 __^
Toto
  • 89,455
  • 62
  • 89
  • 125
0

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.

Community
  • 1
  • 1
Jonathan Benn
  • 2,908
  • 4
  • 24
  • 28