C Sharp - Useful methods - 3 [ IsValidEmailAddress]

Deepak
Posted by in C# category on for Intermediate level | Views : 7461 red flag
Rating: 3 out of 5  
 1 vote(s)

IsValidEmailAddress

This is used to Validating the Email Address
Introduction

IsValidEmailAddress

This is used to Validating the Email Address. 

        /// <summary>
        /// Validating Email Address
        /// </summary>
        /// <param name="lsEmail"></param>
        /// <returns></returns>
        public static bool IsValidEmailAddress( string lsEmail )
        {
            bool lbResult;
            int liPosAtChar = -1 , liPosDotChar = -1;
            lbResult = Utility.IsValidTextBox( lsEmail );
            if ( lbResult )
            {
                liPosAtChar = lsEmail.IndexOf( "@" );
                liPosDotChar = lsEmail.IndexOf( "." );
                if ( ( liPosDotChar == 0 ) || ( liPosAtChar == 0 ) )
                { lbResult = false; }
                if ( lbResult )
                {
                    if ( liPosDotChar == liPosAtChar + 1 )
                    { lbResult = false; }
                    if ( lbResult )
                    {
                        int liposAtcharPlusone = liPosAtChar + 1;
                        if ( lsEmail.IndexOf( "@" , liposAtcharPlusone ) > 0 )
                        {
                            lbResult = false;
                        }
                        else
                        {
                            lbResult = true;
                        }
                    }
                }
            }
            return lbResult;
        }

 

Page copy protected against web site content infringement by Copyscape

About the Author

Deepak
Full Name: Deepak Rao
Member Level: Starter
Member Status: Member
Member Since: 2/4/2009 10:10:50 PM
Country:

www.deepakrao.co.nr
I am an Indian Guy , working as Software Consultant in Bangalore . I am typical average programmer. One of those millions who earn their bread and butter from programming. I'm a software developer by profession, and I have a passion for books and music I am elder son to my parents, with an younger brother. Like most people, it's complicated thing to describe me. I am just inimitable, like many others. But I'll do the best I can to describe myself with words. I know that I am quite a smart person on most things, and also am exceedingly imaginative. I do feel that I am funny, though surely not everyone is funny all the time. My humor would probably best be labeled very wry and sarcastic, though it is quite fun to just plain laugh at the silliness of things lots of times.

Login to vote for this post.

Comments or Responses

Posted by: Vishnupavadi on: 12/8/2011 | Points: 25
hi Deepak, really appreciate your work for validation, its clear & simple way....
everything runs very smoothly... except this IsValidEmailAddress
when i run this i got one error which says "The name 'Utility' does not exist in the current context" . it show near over this line "Utility.IsValidTextBox( lsEmail );"

please help me out... my mail id: vishnupavadi@gmail.com

Posted by: Vijayrhj on: 7/30/2012 | Points: 25
Why do not you use Regular expression to validate the email. This Method is long .
Regex.IsMatch("\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*").
Posted by: Deepak on: 8/1/2012 | Points: 25
Thanks Vishnu.
Its written on 2009!!!

Now time has changed and tips too will change.

I agree with Vijay. We can use regular expression.

Login to post response

Comment using Facebook(Author doesn't get notification)