Results 1 to 9 of 9

Thread: [RESOLVED] email validation [ javaScript ]

  1. #1

    Thread Starter
    Hyperactive Member Aash's Avatar
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    491

    Resolved [RESOLVED] email validation [ javaScript ]

    hi

    i want to know about email validation for the sign up form, want to do it professionally. I've done some using javascript but i want to know the conditions that are applied for email validation when any user registers on any site.



    thanks..

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: email validation [ javaScript ]

    Javascript is used to validate at client side. So, you have to validate at server side too !

    For example, if you are trying to create a "contact us" page, the main fields that you will obtain from the user are:
    • Name
    • Email
    • Message

    By using Javascript, the data entered by the user on these fields can be checked at client side (ie. the Javascript runs and validates in that user's browser). So, this validation is done before submitting the values to the actual server !

    But you have to include code to validate and sanitize the email id, received at the server too. At server side, you'll be doing email validation + sanitizing the email (to remove any email headers injected into it) + cleaning it to prevent any sql injections.

    For email validation, I think, the best approach is to use regular expressions (it's tough one for me )

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3

    Thread Starter
    Hyperactive Member Aash's Avatar
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    491

    Re: email validation [ javaScript ]

    so i need to make a html file with embedded javascript code using RegExp for email validation, and it'll check just before the user presses the submit button (the page will be submitted to user and data will be saved in mysql)?
    am i saying right? and is this the professional way?

  4. #4
    Member PBertie's Avatar
    Join Date
    Aug 2011
    Location
    Newcastle Upon Tyne
    Posts
    55

    Re: email validation [ javaScript ]

    The RegEx that I use is below, it copes with most common email addresses and pretty safe for all personal addresses. It allows all domains which are 2 characters then another 2 characters, but for others they are listed. You'll notice its not a full list, example .biz, .mobi, .jobs, ect. aren't there, you could add them or change it to allow any 3 letter domain.
    Code:
    emailAdd = "[email protected]";
    alert("Email Valid? " + emailAdd.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi) );
    I would say the professional way to do it is like akhileshbc said, have some javascript function that valids the form using the onsubmit event of the form (return false to cancel the submit). But you will also need the server side to do the same validation in case the user has javascript turned off or someone makes their own form and points it at your processing script in order to put invalid data in on purpose.

  5. #5

    Thread Starter
    Hyperactive Member Aash's Avatar
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    491

    Re: email validation [ javaScript ]

    ok, thanks

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: [RESOLVED] email validation [ javaScript ]

    I'd use this regular expression instead:
    Code:
    /^[A-Z0-9\._%+-]+@[A-Z0-9\.-]+\.[A-Z]{2,4}$/i
    You won't need to maintain a list of TLDs this way.
    Like Archer? Check out some Sterling Archer quotes.

  7. #7

    Thread Starter
    Hyperactive Member Aash's Avatar
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    491

    Re: [RESOLVED] email validation [ javaScript ]

    thanks kows, your code is pretty simpler
    one last question, after validating the form with javascript, i have to validate it again before sql statements? (i've some confusion about this)

  8. #8
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: [RESOLVED] email validation [ javaScript ]

    Yes. Validation should always be done on the server side (before your SQL, in ASP.NET/PHP/Java/Ruby/Python/etc). Validation is optional on the client side (in the browser), but can be very helpful for user experience.

    It must always be done on the server side because if I turn JavaScript off and use your form, I'm essentially skipping your validation. JavaScript should never be used to provide "must have" functionality.
    Like Archer? Check out some Sterling Archer quotes.

  9. #9

    Thread Starter
    Hyperactive Member Aash's Avatar
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    491

    Re: [RESOLVED] email validation [ javaScript ]

    Quote Originally Posted by kows View Post
    Yes. Validation should always be done on the server side (before your SQL, in ASP.NET/PHP/Java/Ruby/Python/etc). Validation is optional on the client side (in the browser), but can be very helpful for user experience.

    It must always be done on the server side because if I turn JavaScript off and use your form, I'm essentially skipping your validation. JavaScript should never be used to provide "must have" functionality.
    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width