Vikas Kumar Vikas Kumar - 8 months ago 38
jQuery Question

Why is this regular expression accepting special characters and numbers?

I've created a simple regular expression for email. It is:

/[a-z]+@{1}[a-z]{2,}/

Now the problem is that it's accepting an email like
something;;99@asd
when tested. But it shouldn't allow
;;99
?
I want letters only.

Secondly, please tell me about beginning (
^
) and end (
$
) symbols used in regular expression. I read about them on codeacademy but couldn't understand their purpose. Do they have something to do with my original problem?

EDIT:
Here's the whole jQuery:

<script type="text/javascript">
$(document).ready(function(){
var $pat1=/[a-zA-Z0-9]{5}/;
var $pat2=/[a-z]+@{1}[a-z]{2,}/;
$(".savebutton").click(function(){
var $name=$("input[name='pname']").val();
var $email=$("input[name='pmail']").val();
var $pswd=$("input[name='pswd']").val();
if(($name!="" && $email!="") && $pswd!=""){
if($pat1.test($name)&&$pat2.test($email)){
//ACTIONS
}
}
})
})
</script>

Answer

Try this regex

/^[a-z]+@{1}[a-z]{2,}$/g

Your string must start with a-z (^) and end in a-z($)

^ and $ are for beginning and end