in********@gmail.com wrote:
vy********@gmail.com wrote:
hanseym...@gmail.com wrote:
How do you create a text input box, which shows a default value of 1+
and blocks the user from deleting it? Thanks so much! :))
If you want a text field not editable by user a.k.a. read-only, you
should use respective attribute:
<input type="text" name="super_field" value="1+" readonly="readonly" />
(this is an xhtml syntax)
If you want your form having a parameter but it is not necessary to
show it to the user it's appropriate to use a hidden field:
<input type="hidden" name="...." value="..." />
Is it possible to have your cake and eat it too, that is have a text
input that works as a input, displaying 1+, and allowing the user to
input more, while not allowing 1+ to be deleted? Is this beyond the
limits of JS?
You can use the onchange event handler for this.
http://msdn.microsoft.com/workshop/a...s/onchange.asp
Essentially apply the onchange handler to the form element, make the
handler a function which checks of the user has added text to the form,
or subtracted it, something like:
function checkFormValue(formElement)
{
if(formElement.value.length < 3)
{
formElement.value = "1 + ";
}
}
You can apply this to the form element many ways, the easiest would be
to do:
<input type="text" name="addMoreToMe" id="addMoreToMe" value="1 +"
onchange="checkFormValue(this);" />
Essentially what this does is checks if the length of the form is less
than three, reset the value to the original value. This is a pretty
basic example, as there are ways around it, so be sure to validate the
form properly, but I believe it should be a start for you.
One of the ways around this simple example would be someone pasting
text over the form value, the onchange event handler would never be
fired before the value is less than three as long as whatever they
paste into the form is a string with a length greater than three
(characters.) For example if the form is there, with the "1 +" in it,
and I select the "1 +" and paste "hsdhuhfsfdufh" over it, the above
function I wrote will not correct it.