-1

I am trying to get an email validation to work and can't seem to figure out the problem. I want it to alert if what is entered is not a valid email. The code I attached is the email function and the form code for the email. Help is appreciated thanks!

  {
            var emailfield=document.getElementById("emailaddress").value;  
            var atpos=emailfield.indexOf("@");  
            var dotpos=emailfield.lastIndexOf(".");  
            if (atpos<1 || dotpos<atpos+2 || dotpos+2>=emailfield.length){  
              alert("Please enter a valid email address with an @ and proper domain.")  
              return false;  

            }
            } 

Here is the code for the form.

     <form action='#' method='post' name='f1' id="VPN" onsubmit='return checkButons(this)'> 


  Aspects <br>          
<input type='radio' name='aspect' id='aspect1' value='security' />Security <br>
 <input type='radio' name='aspect' id='aspect2'value='speed' /> Speed<br> 


  Features:<br> 

  <input type='checkbox' name='feat1' id='feat1'value='highspeedvpn' /> High Speed VPN <br> 
   <input type='checkbox' name='feat2' id='feat2' value='transactionguard' />Transaction Guard <br> 
      <input type='checkbox' name='feat3' id='feat3'value='antivirus' /> Antivirus Addon <br> 






        <h3 id="generaltext">Fill out your information below:</h3>

        <p>
        Name:<input name="Name"id='name'size=:50 type="text" required><br>
        </p> 
        <p>

        Email Address:<input name="Emailaddress"id='emailaddress'size=:50 type="text" required><br> 
        </p>
        <p>
        Street Address:<input name="S_Address"id='s_address'size=:50 type="text" required><br></p>
        <p>
        Address 2:<input name="Address_2"id='address_2'size=:50 type="text" required><br></p>      
        <p>
        Zip Code:<input name="Zip"id='zip'size=:50 type="text" required><br>  
        <p>
        City:<input name="City"id='city'size=:50 type="text" required><br>
adilisi
  • 21
  • 7
  • use input type email . It would be a lot easier – ashok poudel Jun 06 '19 at 02:23
  • @ashokpoudel yea it would be a lot easier but I need to do it in javascript sorry. – adilisi Jun 06 '19 at 02:30
  • 3
    You should describe what you expect and what you got. Also the code you posted cannot be executed because it is incomplete. You should provide a short, self-contained correct example that can demonstrate your problems (http://sscce.org/). Another thing is your email validation is far from complete. See (OWSAP Validation Cheat Sheet https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Input_Validation_Cheat_Sheet.md) and (RFC 5321 https://tools.ietf.org/html/rfc5321). – KC Wong Jun 06 '19 at 02:30
  • See also the accepted answer in this post: https://stackoverflow.com/questions/201323/how-to-validate-an-email-address-using-a-regular-expression – KC Wong Jun 06 '19 at 02:34
  • The validation seems to be working fine. What's wrong? – Chaska Jun 06 '19 at 02:43

5 Answers5

1

You can use validation with regular exp -

var email = text_email.value;
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    if (re.test(email)) {
        alert("Error msg");
        return false;
    }
pikachu
  • 36
  • 5
0

Please see following. You can move validation logic to a different function.

JavaScript:

function checkButons(element) {
  ...

  var emailfield = document.getElementById("emailaddress").value;
  var atpos = emailfield.indexOf("@");
  var dotpos = emailfield.lastIndexOf(".");

  if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= emailfield.length) {
    alert("Please enter a valid email address with an @ and proper domain.")
    return false;
  }

  ...
}

HTML:

<form action='#' method='post' name='f1' id="VPN" onsubmit='return checkButons(this)'>
    ....
    <p>
        Email Address:<input name="Emailaddress"id='emailaddress'size=:50 type="text" required><br> 
    </p>
    ....

</form>
0

Instead of using <input name="Emailaddress"id='emailaddress'size=:50 type="text" required> use type email, the browser will do the validations for you,but if you want to extra valdiate in server side you could use RegExp for it

Eduardo Moreno
  • 111
  • 2
  • 9
0

Hi check this codepen https://codepen.io/anon/pen/BeeZBv?editors=1111

It is your JS code and it appears to be working, it may not be catching all possible conditions, but it is definitely working. Can you let me know what is not working?

Here is code

<input name="Emailaddress"id='emailaddress'size=:50 type="text" required>
<button onclick="validate()">Click me</button>

function validate() {
var emailfield=document.getElementById("emailaddress").value;  
            var atpos=emailfield.indexOf("@");  
            var dotpos=emailfield.lastIndexOf(".");  
            if (atpos<1 || dotpos<atpos+2 || dotpos+2>=emailfield.length){  
              console.log("Please enter a valid email address with an @ and proper domain.")  
              return false;  
            }
}
snit80
  • 623
  • 7
  • 13
0

Please keep a submit button on form,as I validated code form is not submitting once you having more input in a form and calling form event onSubmit and form is submitting with one field.

Function checkButons() {
  var emailfield = document.getElementById("emailaddress").value; 
  var atpos = emailfield.indexOf("@");
  var dotpos = emailfield.lastIndexOf(".");

  if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= emailfield.length) {
    alert("Please enter a valid email address with an @ and proper domain.")
    return false;
  }
}

HTML Code :

   <form action='#' method='post' name='f1' id="VPN" onsubmit="checkButons()">
 Email Address:<input name="Emailaddress"id='emailaddress'size=:50 type="text" required>
      Street Address:<input name="S_Address"id='s_address'size=:50 type="text" required>
     <input type="submit"  value="Submit">
 </form>