0

I am trying to validate email address field with Javascript only and I have tested many expressions for validating but every time I am getting error message even after putting valid email format. I am also logging the inputted value and it is giving every time 'false' rather than the email address I put. how can I solve this please ? Thanks in advance. Here is the code:

function database(){
                let name = document.querySelector('#name').value;
                let email = document.querySelector('#email').value;
                let cell = document.querySelector('#cell').value;

                let validation = () => {
                    function validateEmail(sEmail){
                        let filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
                        if (filter.test(sEmail) == false){   
                            alert('Please provide a valid email address');
                            console.log(sEmail); 
                            return false;
                        } else{
                            return true;
                            console.log(sEmail);
                        }
                    }

                    if ( name == '' || name == null  ){
                        alert('Please Fill Name');   
                    } else if ( email == '' || email == null  ){
                        alert('Please Fill Email');
                    } else if ( !(email = '' || email == null) ){
                        validateEmail(email);
                    } else if ( cell == '' || cell == null || isNaN(cell) ){
                        alert('Please Fill Cell');
                    } else {
                        console.log('Function is Running Ok')
                    }
                }
                validation();
            }
            let button = document.querySelector('#button');
            button.addEventListener('click', database);
<div class="form" id="form">
   <h2 class="formtitle">Put the input data</h2>
   <form action="" method="post" id="databaseinput">
    <input type="text" name="name" id="name" placeholder="Name" required />
    <input type="email" name="email" id="email" placeholder="Email" required />
    <input type="number" name="cell" id="cell" placeholder="Cell Number" required />
    <input type="button" name="submit" id="button" value="Submit" />
   </form>
        </div>
Exploring
  • 573
  • 2
  • 6
  • 19
  • What error message are you getting? – Run_Script Jun 18 '20 at 10:59
  • @Run_Script I am getting error alert message of email validation function. – Exploring Jun 18 '20 at 11:01
  • regex for `email` is not quite right, it will not validate every email and you are limiting the domain extension to only 2-4 character, and what about the subdomains, you can also have emails on the subdomain and here is how you validate the email https://stackoverflow.com/a/46181/10249176 – Ace Jun 18 '20 at 11:02
  • @blacksheep Thanks for your reference. I have already checked this and many other answers but didn't get luck. – Exploring Jun 18 '20 at 11:12

0 Answers0