473,395 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how to enable/disable textbox in JavaScript

Javascript help

hi guyz i have a question how can i disable all my textbox in my form if my verification code is not correct? e.g(my verification code is " ng075 " if ng075 is not put in my textbox then the other textbox must be disable..all suggestion are well appreciated..thanks guys in advance i'm really having a hard time right now:(
Apr 19 '12 #1

✓ answered by Bharat383

Expand|Select|Wrap|Line Numbers
  1. <head>
  2. <script type="text/javascript">
  3.     function kk1()
  4.     {
  5.         document.getElementById("txt1").disabled  = true;
  6.     }
  7.     function kk2()
  8.     {
  9.         document.getElementById("txt1").disabled  = false;
  10.  
  11.     }
  12. </script>
  13. </head>
  14. <body>
  15.     <input type="button" name="btn1" id="btn1" onclick="kk1()"  value="Disable" />
  16.     <input type="button" name="btn2" id="btn2" onclick="kk2()" value="Enable" />
  17.     <br />
  18.     <input type="text" name="txt1" id="txt1" value="" />
  19. <body>

6 17293
SeanPD
4
I will offer a JQuery script as the Javascript is a bit long and it is SO simple in JQuery.

First lets include the JQuery Library from Google.
Expand|Select|Wrap|Line Numbers
  1.  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
Next lets assume you verification code input box has an attribute ID="VerifyCD".

We add a functiuon to fire OnBlur (when you leave the verification text box) that will check the code and enable/disable all other input text boxes:

Expand|Select|Wrap|Line Numbers
  1. $('#VerifyCD).blur(function() {
  2.     if ($('#VerifyCD).val() <> "ng075" ) {
  3.         $('input:text').attr('disabled', true);
  4.         $('#VerifyCD).removeAttr('disabled');
  5.     } else {
  6.         $('input:text').removeAttr('disabled');
  7.     }
  8.  
  9. });
How it works:
$('#VerifyCD).blur(function() just means when the user leaves the VerifyCD textbox, run this function.

if ($('#VerifyCD).val() <> "ng075" ) just says if the value they entered is NOT your verify code then
$('input:text') take all text input boxes
.attr('disabled', true); and set their disable attribute to true
Expand|Select|Wrap|Line Numbers
  1. $('#VerifyCD).removeAttr('disabled');
alweays re-enable our VerifyCD text box

Expand|Select|Wrap|Line Numbers
  1. } else {
$('input:text') take all text input boxes
.removeAttr('disabled'); and remove their disable attr
}

I know it is not quite what you asked for, but I hope that helps.

Sean
Apr 19 '12 #2
SeanPD
4
YIKES ...
Everywhere I said $('#VerifyCD)
it should be: $('#VerifyCD')
sorry, I missed the closing quote.
Apr 19 '12 #3
thanks Sean for the rply i'll be in touch with you i'll try it first if it will work..thanks again
Apr 20 '12 #4
Expand|Select|Wrap|Line Numbers
  1. <head>
  2. <script type="text/javascript">
  3.     function kk1()
  4.     {
  5.         document.getElementById("txt1").disabled  = true;
  6.     }
  7.     function kk2()
  8.     {
  9.         document.getElementById("txt1").disabled  = false;
  10.  
  11.     }
  12. </script>
  13. </head>
  14. <body>
  15.     <input type="button" name="btn1" id="btn1" onclick="kk1()"  value="Disable" />
  16.     <input type="button" name="btn2" id="btn2" onclick="kk2()" value="Enable" />
  17.     <br />
  18.     <input type="text" name="txt1" id="txt1" value="" />
  19. <body>
Apr 21 '12 #5
First Create html page with 2 button and 1 textbox.this tutorial learn about the Text box disable using javascript.if Yes Button is click, the TextBox enabled and if No Button is click the TextBox disabled.

this article I will Learn with an example, How to Enable or Disable TextBox on Button Click using JavaScript. if Yes Button is click, the TextBox enabled and if No Button is click the TextBox disabled.
Feb 18 '20 #6
SioSio
272 256MB
I wrote the code as requested by ehpratah.
Expand|Select|Wrap|Line Numbers
  1. <head>
  2. <script type="text/javascript">
  3. function changeDisabled(){
  4.   var obj_1 = document.getElementById("txt1");
  5.   var obj_2 = document.getElementById("txt2");
  6.   var obj_3 = document.getElementById("txt3");
  7.   if(obj_1.value == "ng075"){
  8.     obj_2.disabled = false;
  9.     obj_3.disabled = false;
  10.   }else{
  11.     obj_2.disabled = true;
  12.     obj_3.disabled = true;
  13.   }
  14. }
  15. </script>
  16. </head>
  17. <body>
  18.   <input type="text" value="" id="txt1" />
  19.   <input type="text" value="" id="txt2" disabled />
  20.   <input type="text" value="" id="txt3" disabled />
  21.   <input type="button" value="Button" id ="btn1" onclick="changeDisabled();" />
  22. </body>
Feb 18 '20 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: HolaGoogle | last post by:
Hi all, Can you please tell me what's wrong with my code??? i do have this database in wich i have to field.One is a "yes/no" field and another one is "number" field. To display the yes/no field...
2
by: HolaGoogle | last post by:
Can you please tell me the right way to do this?? it's realy important! thanks in advance... Hi all, Can you please tell me what's wrong with my code??? i do have this database in wich i have...
3
by: Bob Bedford | last post by:
I've this code: function checkdate(FormSubmit){ alert(document.getElementById('Mois').value); if(eval(document.getElementById('Mois'))>0 && eval(document.getElementById('Annee'))>0){...
3
by: Alphonse Giambrone | last post by:
I am trying to enable/disable a requiredfieldvalidator on the client side and am generating an error. I had found some documentation on validation which states that I should be able to...
1
by: hortoristic | last post by:
We are using JavaScript to Enable/Disable certain fields on web pages based on business rules. A simple example is if when using an option type tag, and the two options are Yes and No. If YES...
3
by: MJB | last post by:
I am using the embedded ActiveX IE WebBrowser component in my C# application. I want to be able to Enable/Disable javascripting programmatically. I was wondering if there was a way to achieve...
6
by: M O J O | last post by:
Hi, I have a webform with a textbox and a button. When the textbox is empty (=""), I want the button to be disabled. When the user enters text in the textbox, the button must be enabled. Is...
2
by: Water Cooler v2 | last post by:
How do you enable/disable JavaScript in Internet Explorer v6?
6
by: kfank | last post by:
I have a multi-line TextBox with a vertical scrollbar that logs data from a realtime process. Currently, whenever a new line is added the textbox scrolls to the bottom so you can see the last...
3
by: sanndeb | last post by:
I want to enable/disable controls of a asp.net page against a logged in user's permission. say 'admin' & 'hr' can change user's birth date text-box in a page but others will see the text-box as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.