Change background color of input text field using JavaScript
How do I change the background color of a input text开发者_运维问答 field using java script? I want to insert it in a form validation function like this:
function validateForm()
{
var x=document.forms["form1"]["username"].value;
if (x==null || x=="")
{
document.forms["form1"]["username"].style.backgroundColor = Black
}
}
Also, is there a way to do form validation with php without creating two pages? WHat would you recommend? Using JavaScript or php for validating the user input of a form? I already have a php process form which checks whether the user supplied the correct details like the password etc.
Thank you for any help
You are taking the value of the text input. You need to refer to the control itself:
function validateForm()
{
var ctrl = document.forms["form1"]["username"];
var x=ctrl.value;
if (x==null || x=="") {
ctrl.style.backgroundColor = color;
}
}
精彩评论