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?
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?
Share Improve this question edited Jul 20, 2020 at 15:09 service-paradis 3,3984 gold badges37 silver badges48 bronze badges asked Sep 15, 2013 at 15:36 AskerAsker 852 gold badges2 silver badges11 bronze badges 4- 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 Commented Sep 15, 2013 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 Commented Sep 15, 2013 at 16:31
-
stackoverflow./questions/8484181/… Side note: I remend using
jQuery
rather than javascript (jQuery is javascript and makes life easier). – Dom Commented Sep 15, 2013 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 Commented Sep 15, 2013 at 16:46
2 Answers
Reset to default 7Right 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="firstname" 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('firstname').disabled = true;
};
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 />