0

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>

3 Answers3

2

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

Piyush.kapoor
  • 6,715
  • 1
  • 21
  • 21
0

Your Regular Expression looks fine. I tested it on few test cases.

  1. Symbol ^ Matches the beginning of the string, or the beginning of a line if the multiline flag (m) is enabled. This matches a position, not a character.

  2. Symbol $ Matches the end of the string, or the end of a line if the multiline flag (m) is enabled. This matches a position, not a character.

For example, if you have to match pattern of exactly length 3, Then you can use ^ and $ to denote beginning and end of the pattern.

Niklaus
  • 65
  • 1
  • 7
0

An example of email regex is like this

 /^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/m

for your question,

Please tell me about beginning (^) and end ($) symbols used in regular expression...

In my example regex above, both symbols are best used if your using the multiline (m) modifier in you regex. (^) is used to identify the beginning of the line and ($) is used to identify the end of the line. It does affect your original problem but I would suggest you use this symbols in your regular expression.

Hope me help well.

eztephen
  • 38
  • 5