Disable all fields on a Web form (ASP.net, HTML forms)

Hi,
 
Today we got a requirement to disable all the fields on the ASP.net form based on some conditions.
 
This can be easily done using JavaScript. Here is the code for it.

var inputs = document.getElementsByTagName(“input”);
for(var i = 0; i < inputs.length; i++) {
     // For applying background color and color.
     // inputs[i].style.backgroundColor = “#9fd4fe”;
     // inputs[i].style.color = “black”;
     inputs[i].disabled = true;
}
varselects = document.getElementsByTagName(“select”);
for(var i = 0; i < selects.length; i++) {
     selects[i].disabled = true;
}
vartextareas = document.getElementsByTagName(“textarea”);
for(var i = 0; i < textareas.length; i++) {
     textareas[i].disabled = true;
}
varbuttons = document.getElementsByTagName(“button”);
for(var i = 0; i < buttons.length; i++) {
     buttons[i].disabled = true;
}
varlinks = document.getElementsByTagName(‘a’);
for(var i = 0; i < links.length; i++) {
     links[i].disabled = true;
}

Hope this helps.
 

Happy Coding

Gopinath

Allow only numbers in a text box

Hi,

In most of web applications, we get requirement to allow only number in a textbox.

Here is the JavaScript for it, just call this method onKeyPressv Event.

<HTML>
   <HEAD>
   <SCRIPT language=Javascript>
      <!–
       function isNumberPressed(evt) {
           var charCode = (evt.which) ? evt.which : event.keyCode
           if (charCode > 31 && (charCode 57))
               return false;
           return true;
       }
      //–>
   </SCRIPT>
   </HEAD>
   <BODY>
      <INPUT id=”txtChar”onkeypress=”return isNumberPressed(event)” type=”text” name=”txtChar”>
   </BODY>
</HTML>
 
Hope this helps.


Happy Coding
Gopinath