3

As the title says, i am, trying to disable textbox when i open the file

<form name='registration' action="registration.php" method="post" onSubmit="return formValidation();">
    <table>
        <tr>
            <td>FirstName:</td>     
            <td><input type = "Text" name = "firstname" id="1"></p></td>
        </tr>
    </table>
</form>

and I try ti disable it using the following javascript code:

function formValidation() {
    var fn = document.registration.firstname;
    if(fname_validation(fn)) {}
    document.getElementById("1").disabled = true;
}

function fname_validation(fn) {
    var fn_len = fn.value.length;
    var fn_char = /^[A-Za-z]+$/;
    if (fn_len == 0) {
        alert("first name cannot empty");
        return false;
    }
    if (!fn.value.match(fn_char)) {
        alert("first name must enter alphabet only");
        return false;
    } else {
        return true;
    }
}

Why the text box still does not disable?

service-paradis
  • 3,333
  • 4
  • 34
  • 43
Asker
  • 85
  • 2
  • 2
  • 11
  • First, there is noo radio button. Second, when do you want to disable your text input Right now, the function that make your input disabled is called on form submit. So, your input will be disabled only when you submit your form... probably to late. If you want your input to be disabled at start, your input should look like this – service-paradis Sep 15 '13 at 16:27
  • omg, i wrongly put the title. i want to test how to disable textbox using javascript when i go to the html page. so when i open html file, the textbox is directly disabled – Asker Sep 15 '13 at 16:31
  • http://stackoverflow.com/questions/8484181/how-to-enable-a-disabled-text-field **Side note:** I recommend using `jQuery` rather than javascript (jQuery is javascript and makes life easier). – Dom Sep 15 '13 at 16:32
  • i believe code and references that are given by you guys are working, but i tried to put it on my javascript file and still not working – Asker Sep 15 '13 at 16:46

2 Answers2

7

First, there is no radio button. Second, when do you want to disable your text input? Right now, the function that make your input disabled is called on form submit. So, your input will be disabled only when you submit your form... probably too late. If you want to disable your input from start, your input should look like this

<input type = "Text" name = "firstname" id="1" disabled="disabled" />

Or if you want to disable it with javascript, you have to execute it on load. Something like this:

window.onload = function() {
   document.getElementById('1').disabled = true;
};
service-paradis
  • 3,333
  • 4
  • 34
  • 43
0

If you want the textbox to be disabled on page load, add the disabled attribute to your input

<input type = "Text" name = "firstname" id="1" disabled />
tymeJV
  • 103,943
  • 14
  • 161
  • 157