-2

I tried to create a function for Validate Email in three cases:

  1. check the email text field whether it contains ‘@’ for any email address.
  2. make sure that the email field is not empty 3)it does not contain more than 100 characters.

my problem is that when I press submit, it just checks for ‘@’ even when the field is empty output

this is my code from Apache NetBean:

for form:

<form method="post" action ="" novalidate onsubmit="return ValidateRadioButtons(), ValidateEmail()">

for email:

            <input type = "text" name ="email" id = "email" placeholder="aaa@ucj.edu.sa" onchange="ValidateEmail()" >

for function:

    function ValidateEmail() {
        var email = document.getElementById("email");
        if (email.value.length > 100) {
            window.alert("Email address should not contain more 100 than characters");
            email.focus();
            return false;
        } else if (email.value.indexOf("@") < 0) {
            window.alert("Email address should  contain @ character");
            email.focus();
            return false;
        } else if (email.value.length === 0) {
            window.alert("You should enter Email address");
            email.focus();
            return false;
        } else {
            return true;
        }
    }
j08691
  • 204,283
  • 31
  • 260
  • 272
Ndeer
  • 1
  • 3
  • You check for "@" before you check for empty field. – gre_gor Oct 12 '22 at 19:14
  • @gre_gor: I thought there is a big mistake, but it works... thank you so much! – Ndeer Oct 12 '22 at 19:17
  • Yeah and also just so you know, it enters the else if condition where you check the index of @, because the indexOf() method returns -1 (<0) if the provided char is not present, and it is certainly not present in empty string :D – KateLatte Oct 12 '22 at 19:20
  • While you're at it: if you're `if` statement include a `return` no need for `else if` you can just use `if`. `return` will prevent the else part of the if from happening. – Zargold Oct 12 '22 at 19:21
  • Does this answer your question? [How can I validate an email address in JavaScript?](https://stackoverflow.com/questions/46155/how-can-i-validate-an-email-address-in-javascript) – Henry Woody Oct 22 '22 at 18:40

1 Answers1

0

You need to check if the length is 0 first, then the maximum length. Then you can check the required @ if the previous checks are ok!

function ValidateEmail() {
    var email = document.getElementById("email");
    if (email.value.length ===0 ) {
        window.alert("You should enter Email address");
        email.focus();
        return false;
    } else if (email.value.length > 100) {
        window.alert("Email address should not contain more 100 than characters");
        email.focus();
        return false;
    } else if (email.value.indexOf("@") < 0) {
        window.alert("Email address should  contain @ character");
        email.focus();
        return false;
    } else {
        return true;
    }
}
RealHowTo
  • 34,977
  • 11
  • 70
  • 85