-1

What do i need to do:

-Email field needs to respect the EMAIL format (_@.__), if user doesn't respect it a message should pop up. - Here I couldn't finish, I don't know how.

-Phone field needs to be completed only with digits. - Doesn't work either...and also the vars for this function blocked the whole script ..I don't know what to do to finish the work.

What have i done: -make an input of Email and Phone, DONE

-at most 20 characters email and 15 characters phone, DONE

-at least 1 characters email and 2 character phone, DONE

here is the code:

     <script>
    function checkForm() {

        var div5 = document.getElementById("email1");
        var div6 = document.getElementById("email2");
        var div7 = document.getElementById("email3");
        var div8 = document.getElementById("phone1");
        var div9 = document.getElementById("phone2");
        var div10 = document.getElementById("phone3");

        var email = document.getElementsByName("Email");
        var phone = document.getElementsByName("Phone");

        div5.style.display="none";
        div6.style.display="none";
        div7.style.display="none";
        div8.style.display="none";
        div9.style.display="none";
        div10.style.display="none";

        var query = document.getElementsByName('Phone').value;
        var isNumeric=query.match(/^\d+$/);
        if(isNumeric){
            div10.style.display="inline";
        }     



        if (email[0].value.length>20){
            div5.style.display="inline";
        }           else if (email[0].value.length<1){
                             div6.style.display = "inline";         
                    }

        if (phone[0].value.length>15){
            div8.style.display="inline";            
        }           else if (phone[0].value.length<2) {
                             div9.style.display="inline";
                    }


    function checkEmail() {

            var email = document.getElementById('email');
            var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

            if (!filter.test(email.value)) {
            alert('Please provide a valid email address');
            email.focus;
            return false;
            }
    }


    }

     </script>
  </head>

    <body>
        <form name="mainform">  

        <div id="email">Email</div>
        <input type="text" name="Email">
        <div style="display:none" id="email1">error - max 20 characters.</div>
        <div style="display:none" id="email2">error - at least 1 character.</div>
        <div style="display:none" id="email3">error - respect the format __@__.__</div>

        <div id="phone">Phone</div>
        <input type="text" name="Phone">
        <div style="display:none" id="phone1">error - max 15 characters.</div>
        <div style="display:none" id="phone2">error - at least 2 character.</div>
        <div style="display:none" id="phone3">error - only digits.</div>

        <br>
        <button type="button" onclick="checkForm(); checkEmail();"/>Check form</button>
  • 1
    Possible duplicate of [Validate email address in JavaScript?](http://stackoverflow.com/questions/46155/validate-email-address-in-javascript) – Liam Oct 17 '16 at 16:31

1 Answers1

0

You probably want to use regex for this. With some googling you can find out how you should do that.

You can use this site to test your regex patterns: http://regexr.com/

Maarten Kieft
  • 6,806
  • 3
  • 29
  • 34