dimanche 6 décembre 2015

if condition met, take user to another page

I am having a bit of difficult in figuring out this one. I have couple of conditions for a phone verification. I want to take the user to a different page if the conditions are met... how can I best do this with JavaScript?

Please see code attached.

function validatePhone(){

                var phone  = document.getElementById("phone1").value;
                if(phone.length === 0) {
                                console.log("phone number is required.");
                                producePrompt("Phone number is required.", "messagePrompt", "red");
                                return false;
                        } 

                if(!phone.match(/^\(?([0-9]{3})\)?[-.]?([0-9]{3})[-.]?([0-9]{4})$/)) 
                {
                        producePrompt("Please enter a valid Phone number.", "messagePrompt", "red");
                        return false;
                        }
                        producePrompt("valid Number", "messagePrompt", "green");
                        return true;
                }

                function producePrompt(message, promptLocation, color) {
                        document.getElementById(promptLocation).innerHTML = message;
                        document.getElementById(promptLocation).style.color = color;
                }

                function remove_msg(){
                        producePrompt(" ", "messagePrompt", "red");
                }
<form>
                <p>Please enter your phone number below:</p>
                <input type="tel" name="phone1" id="phone1" placeholder="(000)000-0000" onchange="validatePhone()"/ onmouseover="remove_msg()">
                <label for="" id="messagePrompt"></label>
                <br>
                <input id="button" type="button"  value="send message" onclick="validatePhone()"/>
        </form>

Aucun commentaire:

Enregistrer un commentaire