www.webdeveloper.com
Results 1 to 7 of 7

Thread: Email validation if textbox not empty

  1. #1
    Join Date
    Nov 2007
    Posts
    20

    Email validation if textbox not empty

    Hi

    There is something not quite right with this. Can anyone help?

    I want to say that if the email textbox is not empty, then do the Email validation, which does work on its own. It is part of a function.

    There is something wrong with the first if....???

    if (!(document.frmRegister.email.value.length = 0))
    {
    if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.frmRegister.email.value)))
    {
    messagebox += "You must enter a valid Email address, please try again \n";
    }
    }
    }

    Thanks

  2. #2
    Join Date
    May 2007
    Location
    Cleveland, OH
    Posts
    1,403
    Code:
    if (!(document.frmRegister.email.value.length == 0))
    You forgot a "=" in your comparison.

  3. #3
    Join Date
    Nov 2007
    Posts
    20
    Hi,

    no that is not the answer because the code runs through and doesn't bring up an alert.

    If I cut out this code, then the alert connected with my function relating to the other comparisons works.

    How can I make this bit of code work?

    Thanks

  4. #4
    Join Date
    Mar 2007
    Posts
    44
    i see two opening braces, and three closing braces.

  5. #5
    Join Date
    May 2007
    Location
    Cleveland, OH
    Posts
    1,403
    Code:
    if (document.frmRegister.email.value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/) != null) {
      
    messagebox += "You must enter a valid Email address, please try again \n";
    }
    You dont need to test for length, if they don't enter anything it will fail the regexp anyway.

  6. #6
    Join Date
    Nov 2007
    Posts
    20
    Hi

    It was nearly correct, thanks very much - I think this way is now working,


    if (!(!(document.frmRegister.email.value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/)== null)))
    {
    messagebox += "You must enter a valid Email address, please try again \n";
    }

  7. #7
    Join Date
    Jan 2011
    Posts
    1

    Do not use the javascript shown on this page

    Do not use the JavaScript shown on this web page to validate email addresses. All of the examples shown here are wrong..

    For more information, see http://www.iana.org/domains/root/db/ for a list of valid top level domains. See especially the "gTLDs" section (there is a link to that section at the top of the page). The JavaScript examples on this page assume a top level domain name can only have 2 or 3 characters. That has not been correct for over 10 years, and if you use this JavaScript many people will be unable to enter their valid email addresses into your web page.


    Do not attempt to validate email addresses using JavaScript unless:
    1. You are capable of writing the JavaScript yourself without seeking assistance from a web forum;
    2. You test your code extensively; and
    3. You are ready and able to monitor the IANA list and keep your code up to date;

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles