-1

This is a ready-baked regex that I got from the internet just to see how regex works in JavaScript (and jQuery). Here is my script:

<script>
    $(document).ready(function(){
        var regexp = new RegExp(([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4}));
        $("#submit").click(function(){
            var result = regexp.test( $("#email").val() );
            if(result){
                $("form").submit();
            }else{
                alert("Invalid Email Address");
            }
        });
    });
</script>   

However, the form always submits despite entering a wrong email address.

I have read on how test() works. It know that it returns the right answer and then advances a pointer to the next value. So if it returns true, the pointer points to false. I do not see how that can be a problem here.

what is wrong here? :)

HTML:

<body>
    <center>
        <form name="subscription" method="post" action="http://www.cs.tut.fi/cgi-bin/run/~jkorpela/echo.cgi">
            <input type="text" name="email" size="30" id="email"></input>
            <button id="submit">Submit</button>
        </form>
    </center>
</body>
5
  • create jsfiddle also. Commented Mar 31, 2014 at 6:45
  • 2
    @PratikJoshi With all due respect, I feel you are being a little disrespectful here. Commented Mar 31, 2014 at 6:45
  • Please check console errors first-> Uncaught SyntaxError: Unexpected token ILLEGAL Commented Mar 31, 2014 at 6:47
  • Visit stackoverflow.com/questions/46155/… Commented Mar 31, 2014 at 6:51
  • @LittleChild , With all respect , Why do you feel so ?I mean if you expect good answer ,then you should not assume ,that we know your html, So please post jsfiddle ,and include HTML. Commented Mar 31, 2014 at 6:56

6 Answers 6

2

You need to double escape your Regex. Try this-

var regexp = new RegExp("([\\w-\\.]+)@((?:[\\w]+\\.)+)([a-zA-Z]{2,4})");

Edit

It is always helpful to log the result so that you see if your regexp string is correct, eg:

console.log("([\\w-\\.]+)@((?:[\\w]+\\.)+)([a-zA-Z]{2,4})");
console.log("([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})");
Sign up to request clarification or add additional context in comments.

2 Comments

Done !! Working !! :-)
Few more minutes to go :)
2

Use this instead?

<input type="email" />

1 Comment

Yes, that is an option but the idea is to see how regex work. That's all :)
2

Your regex initialization should be string and it should be escaped.

var regexp = new RegExp("([\\w\\.-]+)@((?:[\\w]+\\.)+)([a-zA-Z]{2,4})");

You can also use:

var regexp = /([\w\.-]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})/;

5 Comments

ah ! I didnt see the tutorial mention that :)
uhm... still doesnt solve the problem.
what do you mean by not solving the problem?
still it shows console error
@AmitJoki : Range out of order in character class
0

write it a regex (note the /)

Regex: var regexp = new RegExp(/([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})/);

Comments

0

In Html5, There is a new attribute type="email" is available.

Instead of using input type="text"

Please try this!

Comments

0

Please try like this:

var regex = /^[a-zA-Z0-9äöüÄÖÜ._-]+@[a-zA-Z0-9äöüÄÖÜ.-]+.[a-zA-Z]{2,4}$/;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.