Results 1 to 2 of 2

Thread: Var in RegExp doesn't work in javascript email vaditation

  1. #1
    Join Date
    Apr 2014

    Var in RegExp doesn't work in javascript email vaditation

    This piece of code suppose to let user inserts a new record if the email is end with @test.com, and it should return false and show alert "Please Enter Correct Email Domain" if the email does not end with @test.com. However, the current code always let user inserts a new record no matter the email end with @test.com or not end with @test.com. There isn't any error message. Here is the code and I hope someone can see what's wrong:

    var javaScriptVar = "test.com";
    var regex = new RegExp("@" + javaScriptVar + "\\s*$");
    alert("Please Enter Correct Email Domain");
    return false;

    I had this direct .test way to do it and it works, but this way cannot let me insert a variable:

    if (!(/@test.com\s*$/.test(document.forms["myForm"]["email"].value))) {


  2. #2
    Join Date
    Dec 2002
    Seattle, WA
    the "." is a modifier in RegEx. You will need to escape it first. example:
    var x = 'foo.bar';
    var re = new RegExp('' +x.replace(/([\.\*\?\-\+\{\}\(\)\[\]\$\^\\\/])/g, '\\$1')+ '');
    Last edited by ShrineDesigns; 04-10-2014 at 05:51 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center