Jump to content

JavaScript validation required field border changed


Alken Jarge
 Share

Recommended Posts

&&&&&&&&&&&&&&&&&&&&&&&&&&& USE IT &&&&&&&&&&&&&&&&&&&&&&&&&&&&&

 

<form name= "reg" id="reg" method="POST" action="" onsubmit="return validate()"> <input id="first" name="first" type="text" value="" placeholder="first"><br /><input id="email" name="email" type="text" value="" placeholder="email"><br /><textarea id="message" name="message" placeholder="message" ></textarea><br /> <input type="submit"></button></form><script type="text/javascript">function validate(){ var formIsValid = true; var first=document.forms["reg"]["first"]; if(first.value == null || first.value == ""){ first.style.borderColor = "red"; formIsValid = false; } var email=document.forms["reg"]["email"]; if(email.value == null || email.value == ""){ email.style.borderColor = "red"; formIsValid = false; } var message=document.forms["reg"]["message"]; if(message.value == null || message.value == ""){ message.style.borderColor = "red"; formIsValid = false; } return formIsValid;}</script>

Link to comment
Share on other sites

            function validate(elem) {                var formIsValid = true;                var curr_form = document.getElementById(elem.id);                for (i = 0; i < curr_form.elements.length; i++)                {                    if (curr_form.elements[i].type != "submit")                    {                        curr_form.elements[i].style.borderColor = "";                        if (curr_form.elements[i].value == curr_form.elements[i].defaultValue || curr_form.elements[i].value == "")                        {                            curr_form.elements[i].style.borderColor = "red";                            formIsValid = false;                        }                    }                }                return formIsValid;            }
<form name= "reg" id="reg" method="POST" action="http://www.qualityzoneinfotech.com/" onsubmit="return validate(this)">

OR target specific required fields

 function validate(elem) {                var formIsValid = true;                var curr_form = document.getElementById(elem.id);                for (i = 0; i < curr_form.elements.length; i++)                {                    if (curr_form.elements[i].type != "submit" && curr_form.elements[i].className == "required")                    {                        curr_form.elements[i].style.borderColor = "";                        if (curr_form.elements[i].value == curr_form.elements[i].defaultValue || curr_form.elements[i].value == "")                        {                            curr_form.elements[i].style.borderColor = "red";                            formIsValid = false;                        }                    }                }                return formIsValid;            }
            <form name= "reg" id="reg" method="POST" action="http://www.qualityzoneinfotech.com/" onsubmit="return validate(this)">                <input id="first" name="first" type="text" value="" placeholder="first" class="required"><br />                <input id="email" name="email" type="text" value="" placeholder="email" class="required"><br />                <input id="other" name="other" type="text" value="" placeholder="other"><br />                <textarea id="message" name="message" placeholder="message"  class="required"></textarea><br />                <input type="submit">            </form>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...