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>