Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    60
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Validate email address in prompt message javascript

    Hi there, I would like to validate the email address typed into the prompt message by the user, but to no avail. Can some kind soul help?

    Code:
    function addOption() 
    {       
        var new = prompt("Enter New Item:"); 
        if (!new == "")
        {
            var answer = confirm ("Are you sure you want to add? ") 
            if (answer)//if answer is true
            {        
                var lst = document.getElementById('lstBx');  // listbox control id
                // Now we need to create a new 'option' tag to add to MyListbox 
                for (var i = 0; i < lst.options.length; i++) 
                {
                    arrTexts = lst.options[i].text; 
                   
                    if (arrTexts.toLowerCase() == newItem.toLowerCase()) 
                    {
                        alert ("That email address is already included in the list - please enter another one.");
                        break;
                    }
                        
                    else
                    {
                        validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
                        strEmail = lst.value;
    
                        // search email text for regular exp matches
                        if (strEmail.search(validRegExp) == -1) 
                        {
                           alert('A valid e-mail address is required.\nPlease retry.');
                           return false;
                        }                
                        var optionNew = document.createElement("option"); 
    
                        optionNew.value = new; // The value that this option will have 
                        optionNew.innerHTML = new; // The displayed text inside of the <option> tags
                                                  
                        // Finally, add the new option to the listbox 
                        lst.appendChild(optionNew);             
                        //sort items in listbox in alpha order
                        arrTexts = new Array();    
                        for(i=0; i<lst.length; i++) 
                        {
                            arrTexts[i] = lst.options[i].text;
                        }     
                                        
                        arrTexts.sort();
                        for(i=0; i<lst.length; i++)
                        {
                            lst.options[i].text = arrTexts[i];
                                lst.options[i].value = arrTexts[i];
                        } 
                    }
                    return false;
                }                                
            }
        }
        else
        {    
            if(new == "")
            {
                alert("Key something to textbox please.");
            }
                        
            else
                alert("Cancelled.");                
        }                              
    }
    Code:
    <select id="lstBx" name="listBox" size="6" style="width: 580px;">  
      
          <option>a@hotmail.com</option>  
      
          <option>b@hotmail.com</option>  
      
          <option>c@yahoo.com</option>  
      
         <option>d@gmail.com</option>  
      
         <option>e@ymail.com</option>  
      
         <option>f@msn.com</option>                                        
      
     </select>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,185
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    That's not the best of email validation regexp's, you know.

    It would for example, allow an email address of
    Code:
        .@..zzzzzzzzzzzzzzzzzzzzzz
    However...it's better than a kick in the teeth.

    But you are using it wrong.

    Replace
    Code:
                      if (strEmail.search(validRegExp) == -1)
    with
    Code:
                      if (! validRegExp.test(strEmail))
    But then look around for a better validator.

    One I commonly use--which is better than that but far from perfect--is
    Code:
         /^(\w[\w\-\'\.]*)+[\w\-\']+\@(\w[\w\=\']*\.)+[a-z]{2,6}$/i
    If you want one the exactly follows the spec, it will be a mile long. Look around with google.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    FWIIW, I always use:-

    Code:
    if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/i.test(emailAddress.value))) {


  •  

    Posting Permissions

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