0

The phone number and the email must be valid.

This is a basic popup contact form without the phone number validation. I would like to validate the phone number. I heard about regex but i´m not sure how to implement in the code.

Since i don´t understand javascrip yet i hope you can help me.

<form action="#" method="post" id="form">

    <h2>Contact Us</h2><hr/>
    <input type="text" name="company" id="company" placeholder="Company"/>

    <input type="text" name="name" id="name" placeholder="Name"/>

    <input type="text" name="email" id="email" placeholder="Email"/>

    <input type="text" name="phone" id="email" placeholder="Phone"/>

    <a id="submit" href="javascript: check_empty()">Send</a>

</form>


function check_empty(){
    if(document.getElementById('company').value == "" 
    || document.getElementById('name').value == ""
    ||document.getElementById('email').value == ""
    ||document.getElementById('phone').value == "" ){
    alert ("Please, fill the fields!");
    }
    else {  
        document.getElementById('form').submit();
    }
}

//function to display Popup
function div_show(){ 
    document.getElementById('abc').style.display = "block";
}

//function to check target element
function check(e){ 
    var target = (e && e.target) || (event && event.srcElement); 

    var obj = document.getElementById('abc'); 
    var obj2 = document.getElementById('popup'); 

    checkParent(target)?obj.style.display='none':null; 
    target==obj2?obj.style.display='block':null; 

} 

//function to check parent node and return result accordingly
function checkParent(t){ 
    while(t.parentNode){ 
        if(t==document.getElementById('abc'))
            { 
                return false 
            }
        else if(t==document.getElementById('close'))
            {
                return true
            } 
        t=t.parentNode 
    } 
    return true 
} 
slavoo
  • 5,798
  • 64
  • 37
  • 39
Shaka21
  • 3
  • 3
  • 2
    Possible duplicate of [Validate phone number with JavaScript](https://stackoverflow.com/questions/4338267/validate-phone-number-with-javascript) – Turnip Aug 18 '17 at 12:20
  • you may want to try this jQuery plugin https://github.com/jackocnr/intl-tel-input – kevinSpaceyIsKeyserSöze Aug 18 '17 at 12:21
  • Using a link to run validation and submit the form is not a good idea, it can be submitted without clicking the link or running the validation. You have not provided any information on how to validate the phone number (there are many different schemes around the world) nor shown any attempt at it. – RobG Aug 18 '17 at 12:29

0 Answers0