Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

validate email or phone js

    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 validatePhone(phone) {
        // 880-1710-336617 or 8801710336617
        var re = /^(?:(?:+|00)88|01)?d{11}$/;
        return re.test(phone);
    }

    function validate() {

        document.getElementById('email_error').innerHTML = "";
        field = $('#email_phone').val();
        //If not an email AND not a phone
        if (validateEmail(field)) {
            //alert('Email Passed');
            //onChange();
            document.getElementById('email').value = document.getElementById('email_phone').value;
            document.getElementById('phone').value = "";
            //return true;
        } else if (validatePhone(field)) {
            //alert('Phone Passed');
            document.getElementById('phone').value = document.getElementById('email_phone').value;
            document.getElementById('email').value = "";
            //return true;
        } else {
            //alert('Email or Phone Error');
            document.getElementById('email').value = "";
            document.getElementById('phone').value = "";
            let text;
            text = "<strong><span style='color:red'> Email or Phone number Invalid</span></strong>";
            document.getElementById("email_error").innerHTML = text;
            return false;
        }

        // Password Match
        var password = document.getElementById('password').value;
        var confirm_password = document.getElementById('password_confirm').value;

        document.getElementById("password_error").innerHTML="";
        if(password==""){
            document.getElementById("password_error").innerHTML="Please enter password";
            return false;   
        }

        if (password != confirm_password) {
            let text1;
            text1 ="<strong><span style='color:red'>The password and its confirm are not the same </span></strong>";
            document.getElementById("password_match").innerHTML = text1;
            return false;
        }

            // Clear Email field value if change
            $("#email_phone").change(function() {
                document.getElementById('email').value = "";
            });
            // Clear Phone field value if change
            $("#email_phone").change(function() {
                document.getElementById('phone').value = "";
            });
            $("#email_phone").change(function() {
                document.getElementById('password_error').value = "";
            });

    }
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #validate #email #phone #js
ADD COMMENT
Topic
Name
7+9 =