Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disabling textbox text input only

Status
Not open for further replies.

chrismassey

Programmer
Aug 24, 2007
264
GB
Hello,
I have a javascript which disables a textbox when the page loads. However, it disables every elements of the textbox including its value. I just want to stop the user being able to input or change text within, and just to view the text that is in there already. When they click a submit button, I use the value from the textbox, but if its disabled then the value cannot be used...

(Javescript in <head>)
Code:
<script language="javascript">
function enableField()
{ 
document.form1.OpenText1.disabled=false;
document.form1.OpenText2.disabled=false;  
}
</script>
<script language="javascript">
function disableField()
{ 
document.form1.OpenText1.disabled=true;
document.form1.OpenText2.disabled=true; 
}
</script>

(form in <body>)
Code:
<FORM ACTION="script.pl" METHOD=POST NAME="form1">
<p><input type="submit" value="+ Directory" name="create_directoryb" target="_blank">&nbsp;&nbsp;&nbsp;<input type="submit" value="+ Page" name="create_pageb" target="_blank">&nbsp;&nbsp;&nbsp;<input type="submit" value="- Direc/Page" name="delete_direc_pageb" target="_blank">&nbsp;&nbsp;&nbsp;<input type="submit" value="Rename Direc/Page" name="rename_direc_pageb" target="_blank"></p>
<p><font size="2" face="Arial">Path input: </font><font size="2" face="Arial">| </font><a href="javascript:enableField()"><font size="2" face="Arial">Enabled</font></a><font size="2" face="Arial">
</font><a href="javascript:enableField()"><a/></a><font size="2" face="Arial">|</font> <a href="javascript:disableField()"><font size="2" face="Arial">Disabled</font></a><font size="2" face="Arial">
|</font><a href="javascript:disableField()"><a/></p>
<p><input type="text" name="OpenText1" size="50" value="z" disabled="false"><input type="submit" value="Open" name="openb1"></p>
<p><input type="text" name="OpenText2" size="50" disabled="false"><input type="submit" value="Open" name="openb2"></p>
</FORM>


Thank you for all your help
 
The value does change, the value is simply the path to a location on the server. I want certain users to be able to change the path themselves, however I don't want others for security reasons. This was the simplist method I could think of. I was thinking I could just print the path in text and carry it within a hidden field, but this is a cleaner method...

I will try your method now, thanks alot :)
 
A word of caution - you don't rely on client-side code for security. It would be too easy for someone to save the form to their computer, change whatever they want, and then submit the altered form. Or they could just type [tt]javascript:void(document.formName.fieldName.readOnly=false)[/tt] in the address bar to circumvent your 'security'. You should always validate that the users have access to perform certain functions on the server.

Adam
 
Thanks adam...

The majority of my code is written in Perl/CGI, and i'm using html as output. Therefore i'm using Perl script methods to secure the script and html output so that users are unable to view or modify then submit data.

Chris
 
Hi,
I suggest you use a <span> with ID to present the fields the user can not change.

You store the actual values in hidden input fields..
<input type="hidden"

Olav Alexander Mjelde
Admin & Webmaster
 
Hi

Olav said:
I suggest you use a <span> with ID to present the fields the user can not change.

You store the actual values in hidden input fields..
<input type="hidden"
Indeed, that is stronger than [tt]disabled[/tt] and [tt]readonly[/tt]. But the visitor can paste this into the browser's location bar, or execute it as bookmarklet :
Code:
javascript:h=document.getElementsByTagName('input');for(i=0;i<h.length;i++)if(h[i].type=='hidden')with(h[i]){type='text';title=name}void(0)
( Ignore the fact that Explorer seems to be unable to change an [tt]input[/tt]'s [tt]type[/tt] like that. It can be solved with a bit of DOM scripting. )

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top