11 Replies - 726 Views - Last Post: 17 August 2011 - 01:35 PM Rate Topic: -----

#1 MjM8082  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 22
  • Joined: 17-August 11

Can't Get My Email Validator to work

Posted 17 August 2011 - 11:30 AM

Validator will not work for my email not sure why.

Here is my code for my Validator...


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace ContactMgr
{
    public class Validator
    {
        public static bool ContainsBadWords(string temp)
        {
            bool result = false;
            if (temp.Contains("poopy"))
            {
                result = true;
            }
            return result;
        }


        public static bool IsEmailValid(string strEmail)
        {
            bool result = true;

            //Look to make it false if the info is invalid
            if (strEmail.Length < 8)
            {
                result = false;
            }
            else if (!(strEmail.Contains("@")))
            {
                result = false;
            }
            else if (!(strEmail.Contains(".")))
            {
                result = false;
            }
            else if (strEmail.IndexOf("@") < 2)
            {
                result = false;
            }
            else if (((strEmail.IndexOf(".", (strEmail.IndexOf("@") + 1)) - strEmail.IndexOf("@")) - 1) < 2)
            {
                result = false;
            }
            else
            {
                result = true;
            }

            return result;
        }
    }
}





Here is my code for my Person.cs




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ContactMgr
{
    public class Person
    {
        private string fName;
        private string mName;
        private string lName;
        public string City;
        public string email;
        public string Prefix;
        public string State;
        public string Street;
        public string Street2;
        public string Zip;

        public string feedback = "";



        public Person()
        {
        }

        public string FName
        {
            get
            {
                return fName;
            }
            set
            {
                if (Validator.ContainsBadWords(value))
                {
                    feedback += "ERROR: Bad Word in First Name\n";
                }
                else
                {
                    fName = value;
                }
            }
        }

        public string MName
        {
            get
            {
                return mName;
            }
            set
            {
                if (Validator.ContainsBadWords(value))
                {
                    feedback += "ERROR: Bad Word in Middle Name\n";

                }
                else
                {
                    mName = value;
                }
            }
        }

        public string LName
        {
            get
            {
                return lName;
            }
            set
            {
                if (Validator.ContainsBadWords(value))
                {
                    feedback += "ERROR: Bad Word in Last Name\n";

                }
                else
                {
                    lName = value;
                }
            }
        }

        //Email
        public string Email
        {
            get { return email; }
            set
            {
                if (Validator.IsEmailValid(value))
                {
                    email = value;
                }
            }
        }

        public string Feedback
        {
            get
            {
                return feedback;
            }
            set
            {
                if (Validator.ContainsBadWords(value))
                {
                    feedback += "ERROR: Bad Word in Feedback\n";
                }
                else
                    feedback = value;
            }
        }
    }

}





Here is my code for the form.cs




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ContactMgr
{
    public partial class Form1 : Form
    {
        //private class Person
        //{
           // private   string Prefix;
           // private string FName;
            //private string MName;
            //private string LName;
            //private string Street;
           // private string Street2;
           //private string City;
           // private string State;
           // private string Zip;
            
           // private string Email;

            //public string Feedback = "";

           

            


           // public Person()
           // {
            //    Prefix = "Mr.";


          //  }


      //  };

        public Form1()
        {
            InitializeComponent();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        
        {


            Person temp;

            temp = new Person();


            temp.FName = txtFName.Text;
            temp.MName = txtMName.Text;
            temp.LName = txtLName.Text;
            temp.Street = txtStreet.Text;
            temp.Street2 = txtStreet2.Text;
            temp.City = txtCity.Text;

            temp.State = txtState.Text;
            temp.Zip = txtZip.Text;
            
            temp.email = txtEmail.Text;

            if (temp.LName == "McCarthy")
            {

                DisplayInfo(temp);
            }

           // if (temp.FName.Length > 0 && temp.LName.Length > 0)
            if (!String.IsNullOrWhiteSpace(temp.FName) && !String.IsNullOrWhiteSpace(temp.LName))


            {
                DisplayInfo(temp);
            }
            else if (!String.IsNullOrWhiteSpace(temp.Feedback))
            {
                lblFeedback.Text = temp.Feedback;
            }
            {
                {
            dtpStartTime.CustomFormat = "MM/dd/yyyy HH:mm:ss tt";
            dtpStartTime.MaxDate = DateTime.Now.AddYears(1);

            MessageBox.Show(dtpStartTime.Value.ToString());
        }

                
            }

        }


       private void DisplayInfo(Person temp)
       {
            lblFeedback.Text = temp.Prefix + " " + temp.FName + " " + temp.MName + " " + temp.LName + " " + temp.Street + " " + temp.Street2 + " " + temp.City + " " + temp.State + " " + temp.Zip + " " + temp.email;

        }

        private void DisplayInfo()
        {
            lblFeedback.Text = "unknown person, need more data";

        }


    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Can't Get My Email Validator to work

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9097
  • View blog
  • Posts: 34,181
  • Joined: 12-June 08

Re: Can't Get My Email Validator to work

Posted 17 August 2011 - 11:35 AM

I wondered how long it would take for that question to migrate from social.msdn to here.

Explain what you mean by "will not work". Is there an exception - if so what? Which line? Is it producing behavior you were nto expecting? If so what is it doing? What should it be doing?

Have you tried "debugging" the code? Granted this link is VB.NET the principles for interacting with the IDE are the same.
Was This Post Helpful? 0
  • +
  • -

#3 MjM8082  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 22
  • Joined: 17-August 11

Re: Can't Get My Email Validator to work

Posted 17 August 2011 - 11:40 AM

The validator is suppose to read and make sure that the user enters a valid email address. If you go to my validator code you can see that.

Next when I run my form and type a Invalid email address.. for example A email address without "@" it still posts the email. When it should be saying... "Email is invalid"
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9097
  • View blog
  • Posts: 34,181
  • Joined: 12-June 08

Re: Can't Get My Email Validator to work

Posted 17 August 2011 - 11:47 AM

Well nut up, put a break point at:

public static bool IsEmailValid(string strEmail)


and step through your code seeing which condition it is failing on and solving the problem from there. I mean debugging is total "never-programmed-never-ever" issue and not something I would expect to a person who built their own PHP forum.
Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Can't Get My Email Validator to work

Posted 17 August 2011 - 11:53 AM

The way you are trying to validate the email address is a ton more work than it needs to be. Just use one of the many RegEx formula for that.

Plus your logic isn't valid. You start out with the result being true. That's bad. You should always start out with something like this being false, then set it to true if everything works. Otherwise you open yourself to a false positive for just about any kind of bad logic. For example:
if (strEmail.Length < 8)

Why? a@a.com is totally legal and less than 8 characters.
At this point none of your other conditions are checked yet you return True.

Your logic that every other condition check is an else is also a problem. Not to mention that you end with and else true situation. Why? You already set your default to true so why would you do it again?

(((strEmail.IndexOf(".", (strEmail.IndexOf("@") + 1)) - strEmail.IndexOf("@")) - 1) < 2)

This is also not a good check for a completely valid email address like:
someone@someplace.com.au


If you had placed a breakpoint at the start of the method and watched the execution you would have seen all of this.

See FAQ #5 for C# debugging tutorials


FAQ (Frequently Asked Questions - Updated Aug 2011

Spoiler


Let me also throw in a couple tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.
    btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like Index, TimeOut, Row, Column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are [url="http://msdn.microsoft.com/en-us/library/xzf533w0(v=vs.71).aspx"]naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.

    For example: Your btnSubmit_click method
  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    Spoiler

This post has been edited by tlhIn`toq: 17 August 2011 - 11:56 AM

Was This Post Helpful? 0
  • +
  • -

#6 MjM8082  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 22
  • Joined: 17-August 11

Re: Can't Get My Email Validator to work

Posted 17 August 2011 - 11:57 AM

View Postmodi123_1, on 17 August 2011 - 11:47 AM, said:

Well nut up, put a break point at:

public static bool IsEmailValid(string strEmail)


and step through your code seeing which condition it is failing on and solving the problem from there. I mean debugging is total "never-programmed-never-ever" issue and not something I would expect to a person who built their own PHP forum.



Okay, I stopped reading your replies 2 hours. go away... THANKS!
Was This Post Helpful? -5
  • +
  • -

#7 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9097
  • View blog
  • Posts: 34,181
  • Joined: 12-June 08

Re: Can't Get My Email Validator to work

Posted 17 August 2011 - 12:01 PM

What I said is a valid point. If you want to grow as a developer then debugging is a crucial skill. Being able to sit down and logically follow your own code to find the crux is not something that happens once in a blue moon but ALL-THE-TIME. Here's an example of a great debugging situation. Hell - half the fun was already taken out of it when I gave you an idea where to *PUT* the break point.

I guess I am having a hard time to phrase answers to give you the best information for what would be an elementary debugging issue countered against someone who wrote a php forum.
Was This Post Helpful? 1
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Can't Get My Email Validator to work

Posted 17 August 2011 - 12:03 PM

I rarely -1 someone. Even newbies like yourself. But that was just completely rude and uncalled for.

The man made serious and good suggestions to you and you give him such attitude and smart-mouthed comments like some kid with an attitude problem? WTF?

I won't speak for anyone else, but you you just made my "Do not help" list.
Spoiler

This post has been edited by tlhIn`toq: 17 August 2011 - 12:08 PM

Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4469
  • View blog
  • Posts: 7,783
  • Joined: 08-June 10

Re: Can't Get My Email Validator to work

Posted 17 August 2011 - 12:18 PM

View PosttlhIn`toq, on 17 August 2011 - 01:53 PM, said:

The way you are trying to validate the email address is a ton more work than it needs to be. Just use one of the many RegEx formula for that.


Actual RFC 822 Email Address regex:

Spoiler


Source
Was This Post Helpful? 0
  • +
  • -

#10 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Can't Get My Email Validator to work

Posted 17 August 2011 - 12:25 PM

View PostMjM8082, on 17 August 2011 - 02:57 PM, said:

Okay, I stopped reading your replies 2 hours. go away... THANKS!


Not going to downvote you, but you would be much better helped if you stopped ignoring good replies. :)

@Tilq/Curtis: FYI, there is no 100% emails regex validator. Just use one that's "good enough" for your needs.

http://stackoverflow...email-addresses

This post has been edited by Sergio Tapia: 17 August 2011 - 12:27 PM

Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Can't Get My Email Validator to work

Posted 17 August 2011 - 01:18 PM

tlhIn`toq said:

There are many different kinds of nebiew posters on DIC.

By far the majority of newbies are people asking for code that solves their needs/homework. It doesn't seem to matter to these people that DIC has posted all over the place "We won't do you homework" and "You need to provide your good faith effort to code a solution to your problem." These people have already demonstrated with their first post a complete disrespect for the site rules and by extension the people on the site. They have also demonstrated their view of the volunteers here as being their personal code generation servants. Since these people have already insulted everyone on DIC with their arrogance it is not surprising that I or anyone else respond in the same manner in which they started their topic.

Next come the people that aren't exactly looking for a complete code dump of the solution. These are the people that are just too lazy to google and experiment and do tutorials or too lazy to learn before trying. They just start typing and *think* they know what a function should do based on nothing more than the name of the function. Then when it doesn't work as expected they declare it broken and ask for help to get around the *problem*, without every trying to look it up on MSDN or crack open a book. This doesn't disrespect the site, but it does disrespect the volunteers by reducing them to the OP's research slaves. Again, it's not unreasonable to expect a volunteer to respond with a tone of "I'm not your goggle-bot. Try looking it up and following the example."

Next comes the rare person who has made an effort. This highly prized newbie is working through a book or on-line tutorial... get's stuck {usually on something poorly documented}... takes it upon themselves to research... does their own trial and error... But still can't get an answer so they are left with having to ask for help. In my book these are the only type of people that will ever amount to anything in the coding world (but that is personal opinion). This rare third type of newbie is someone that we all like to see, want to see more of, and we do our best to give as much attention and patience as we can muster. Can I get a 'Hell ya!' ?

It should also be considered that being frank and honest with someone is not the same as being rude. Just because a volunteer doesn't blow rainbows and sunshine up someone's skirt doesn't mean they are rude. Honestly advising a student they lack any amount of understanding on a topic, and they need to seek out the teacher or tutor is not rude. It is frank honesty, aimed at convincing them to seek help so they don't become further lost and waste more time and money while loosing out on an education.

As frank as I have been with a lot of newbies, I don't think you could find an instance where even *I* have called someone stupid. Lazy perhaps. But it is not in my nature to call someone stupid. I know I have suggested (more than once even) that someone might not be equipped for programming as a profession. Some people might think that rude. That may not be what someone wants to hear. But it could be what they *need* to hear. Some people really don't have what it takes. I would not be a good airline pilot or food server: I'd kill someone by the end of a day at food service. Some people are not equipped to be cops, or air traffic tower controllers. Should those people receive nothing but encouraging but polite lies? I think not. At some time all people have to grow up and accept frank honesty and a realistic self-image of their capabilities.

Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4469
  • View blog
  • Posts: 7,783
  • Joined: 08-June 10

Re: Can't Get My Email Validator to work

Posted 17 August 2011 - 01:35 PM

View PostSergio Tapia, on 17 August 2011 - 02:25 PM, said:

View PostMjM8082, on 17 August 2011 - 02:57 PM, said:

Okay, I stopped reading your replies 2 hours. go away... THANKS!
Curtis: FYI, there is no 100% emails regex validator. Just use one that's "good enough" for your needs.

http://stackoverflow...email-addresses


I re-read what I posted, and it seemed like I was correcting tlhintoq. Actually, I was more joking about how the Perl module for regex validating a proper email address was ridiculously long, and still wasn't perfect.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1