开发者

Change background color of input text field using JavaScript

开发者 https://www.devze.com 2023-04-09 19:10 出处:网络
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:

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;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消