Javascript - email validation does not work

email domain validation in javascript
email validation in javascript using regular expression with explanation
simple email validation in javascript
email validation in javascript w3schools
email'' id validation in javascript without regular expression
email regex
email validation in jquery
email validation in javascript - stack overflow

I have the following HTML script with CSS and Javascript in it. I have a simple javascript function which should validate the email I have entered. But the javascript function does not get called at all. Can someone help ?

function validateForm() {
  var x = document.getElementById("email").value;
  alert(x);
  var atpos = x.indexOf("@");
  var dotpos = x.lastIndexOf(".");
  if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {
    alert("Not a valid e-mail address");
    return false;
  }
}
<h1>DELIVERY DETAILS</h1>

<form id="deliveryDetails">
  First name:<br>
  <input type="text" name="firstname"><br> Last name:<br>
  <input type="text" name="lastname"><br> e-mail:
  <br>
  <input type="text" id="email"><br> telephone number:<br>
  <input type="text" name="telephone number"><br> order number:<br>
  <input type="text" name="order number"><br> order date:<br>
  <input type="text" name="order date"><br><br>
  <input type="submit" value="submit" onsumbmit="return validateForm();">

</form>

The submit button does not have an onsubmit event, the form does, add the event to the form instead, and the code does get executed:

function validateForm() {
  var x = document.getElementById("email").value;
  alert(x);
  var atpos = x.indexOf("@");
  var dotpos = x.lastIndexOf(".");
  if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {
    alert("Not a valid e-mail address");
    return false;
  }
}
<h1>DELIVERY DETAILS</h1>

<form id="deliveryDetails" onsubmit="return validateForm();">
  First name:<br>
  <input type="text" name="firstname"><br> Last name:<br>
  <input type="text" name="lastname"><br> e-mail:
  <br>
  <input type="text" id="email"><br> telephone number:<br>
  <input type="text" name="telephone number"><br> order number:<br>
  <input type="text" name="order number"><br> order date:<br>
  <input type="text" name="order date"><br><br>
  <input type="submit" value="submit" >
</form>

Javascript form & email validation not working - JavaScript, indexOf("@")==-1 || document.form-validate.email.value.indexOf(".")==-1) { alert("​Thats not a valid email address or doesnt exist!"); return false  In general, Email form validation we can validate name, password, email address, mobile number, etc. Client-side validation overcomes the client from knowing about the form is correct or not before submitting the page. How does Email Validation Work in JavaScript? Validating email address is really important while validating the HTML form page

Definitely, I believe you can use HTML5 advantage, you can use input email type and use CSS for its validation:

<input class="email" type="email"/>

And put related CSS for valid or invalid:

.email:valid {
    color: green; //for example
}

.email:invalid {
    color: red; //for example
}

But in your case, I don't read your function, and I don't know why you do not use RegExp for check validation.

You can bind a click event to your submit button:

<input id="submit" type="submit" value="submit"/>

And for attaching click event:

document.getElementById('submit').addEventListener(`click`,() => {
    validateForm();
}, false);

Your validateForm is not optimized. you can write is like below:

const validForm = (emailString) => {
    return /^[a-z0-9][a-z0-9-_\.]+@([a-z]|[a-z0-9]?[a-z0-9-]+[a-z0-9])\.[a-z0-9]{2,10}(?:\.[a-z]{2,10})?$/.test(emailString);
}

JavaScript : email validation, is a string (a subset of ASCII characters) separated into two parts by @ symbol. a "personal_info" and a domain, that is personal_info@domain. Validation is a method to authenticate the user. JavaScript provides the facility to validate the form on the client-side so data processing will be faster than server-side validation. It is preferred by most of the web developers. Through JavaScript, we can validate name, password, email, date, mobile numbers and more fields.

onsumbmit="return validateForm();"

On sumbmit? You've invented a word there. In any case, that event won't fire the function, because onsumbmit isn't a valid event.

Also, put the event on the form (which 'owns' the event), not the button. i.e.

<form id="deliveryDetails" onsubmit="return validateForm();">

Email Validation, domain name must start with “A-Za-z0-9-“, follow by first level Tld (.com, . Front-end email validation is about figuring out if the syntax is correct, not if the email address is valid. With that said, we’ll start off with the most permissive solution and work our way towards the other end. function emailIsValid (email) { return / \ S + @\ S + \. \ S + /. test (email) }

You can set the type to email without any extra validation code e.g

<input type="email"  required/>

Or use regex

        function validateEmail(email) {
            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,}))$/;
            return re.test(email);
        }

    function validate() {
      var email = $("#email").val();

      if (validateEmail(email)) {

      }

$("#validate").bind("click", validate);

HTML

<form>
  <p>Enter an email address:</p>
  <input type="text" id='email'>
  <button type='submit' id='validate'>Validate!</button>
</form>

How to validate email address with regular expression – Mkyong.com, Validate.js is an open source component of Wrapp and is licensed under the MIT The default message is is not a valid email and as usual you can override it  Validating email is a very important point while validating an HTML form. In this page we have discussed how to validate an email using JavaScript : An email is a string (a subset of ASCII characters) separated into two parts by @ symbol. a "personal_info" and a domain, that is personal_info@domain. The length of the personal_info part may be

Use the button type ="submit" & prevent the default behavior of the button using preventDefault

function validateForm(e) {
  e.preventDefault();
  var x = document.getElementById("email").value;
  var atpos = x.indexOf("@");
  var dotpos = x.lastIndexOf(".");
  if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {
    alert("Not a valid e-mail address");
    return false;
  }
}
<h1>DELIVERY DETAILS</h1>
<form id="deliveryDetails">
  First name:<br>
  <input type="text" name="firstname"><br> Last name:<br>
  <input type="text" name="lastname"><br> e-mail:
  <br>
  <input type="text" id="email"><br> telephone number:<br>
  <input type="text" name="telephone number"><br> order number:<br>
  <input type="text" name="order number"><br> order date:<br>
  <input type="text" name="order date"><br><br>
  <button type="submit" onclick="validateForm(event)">Submit</button>
</form>

[Solved] Javascript email validation problem in MVC, Front-end email validation is about figuring out if the syntax is correct, not re-​thinking your approach if the above examples don't work for you. Automatic HTML form validation does not work in Internet Explorer 9 or earlier. Data Validation Data validation is the process of ensuring that user input is clean, correct, and useful.

validate.js, The easiest and safest way to check whether an email address is well-formed is to use PHP's filter_var() function. In the code below, if the e-mail address is not  Javascript Form Validation not working. 9 Years Ago. I have done this before, but for some reason it doesn't want to work with my if statementsit keeps submitting the form when nothing is filled out. It's the validation for an html form, and there are checkboxes that display certain parts of the form.

How to validate an email address in JavaScript (2019), For example, if your form has a box for the user to type their email address, you might Now let's take a look at the JavaScript form validation function that does the If the user has not completed more than one field, then they will see an alert​  My contact form has been validating the name, email and comments box successfully using javascript until I added a captcha code to my contact form today (copy and pasting the captcha code from

PHP Forms Validate E-mail and URL, ( period, dot or fullstop) but it should not be the first or last character and should not come one after the other. The domain name consists of. letters  Standard email address validation uses client-side Javascript to perform rudimentary tests that let a lot of bogus email addresses pass unnoticed. Not so with advanced email address validation Web services. Standard email address validation, using basic client-side validation Javascript, only checks the syntax of an email address. It assures that an email address contains a box name, an @-symbol, a domain name and a minimum character count.

Comments
  • I upvote you because this question is so cute for answering. your code style is a little old and can be in new styles.
  • @ashani123, yeah it works but your code is not optimized.
  • @AmerllicA is right, you should also check out the other answers.
  • @Luca, Absolutely you deserved to have tick mark, but I'm very sensitive on code optimization. that code has no pure function and is written like legacy codes.
  • You should then also set the email input to required, because otherwise you can just not enter an email at all